Skip to content

Instantly share code, notes, and snippets.

Last active June 1, 2023 02:58
Show Gist options
  • Save sophoah/7b7691b23af381b43a974ed5439dcf8c to your computer and use it in GitHub Desktop.
Save sophoah/7b7691b23af381b43a974ed5439dcf8c to your computer and use it in GitHub Desktop.
Cheat Sheet series - git

Git cheat Sheet

working with branches

1 - create new branch

git checkout -b new_branch_name

2 - push (new) branch to remote repo (when the branch doesn't exist at the origin repo)

git push -u origin feature_branch_name

origin is the repo configured as seen in git remote -v

3 - merge new branch to master

git checkout master
git pull #get latest master update
git merge new_branch_name

4 - resolve merge conflict (after doing a git merge)

git status #will show the conflicting file
# open the conflicting file and save the version you want. VSC will allow you to click and save
# commit the change then push

5 - list branches

 git show-branches
 git branch -a

6 - delete branches

git branch -d feature/login

add -f for unmerged changes

7 - delete remote branches

git push origin --delete feature/login

8 - git rebase


          A---B---C topic
    D---E---F---G master
git checkout topic
git rebase master

will transform to

                  A'--B'--C' topic
    D---E---F---G master

9 - push to another branch

git push <remote> <branch with new changes>:<branch you are pushing to> 


git push origin branch1:branch2


1 - removing commit

last commit:

git reset --hard HEAD^

last n commit:

git reset --hard HEAD~n

revert to a specific commit

git reset --hard <tag/branch/commit id>


1 - create and push tag

# create
git tag -a <tagname> -m 'my annotation'
# push
git push origin <tagname>

2 - remote tag

# local
git tag -d <tag_name>
# remote
git push --delete origin <tagname>

How Switch your remote from HTTP to SSH

0 - verify the current remote settings

git remote -v

1 - Change your remote's URL from HTTPS to SSH with the git remote set-url command

git remote set-url origin

2 - Verify the remote URL has changed and https is not used

git remote -v
> origin (fetch)
> origin (push)

How to quickly reset your project with an upstream project ignoring all your local changes

git reset --hard upstream/main
git pull upstream main

Getting a PR before merge

git fetch origin pull/<ID>/head && git checkout FETCH_HEAD

How to synched your forked project

1 - Add a remote to the original "upstream" project

git remote add upstream<USER>/<REPONAME>.git

2 - Verify the new upstream repository you've specified for your fork

git remote -v

3 - fetch all the update on the uptream remote repo

git fetch upstream

4 - Check out your fork's local master branch

git checkout master

5a - Merge the changes from upstream/master into your local master branch

git merge upstream/master

5b - pull and sync only without merging

git pull upstream master --rebase

This brings your fork's master branch into sync with the upstream repository, without losing your local changes.

6 - Push the changes to your own project

git push origin master

cherry pick commits

find the base commit then do

git rebase -i <commitno>

edit the lines you wanna keep/remove

if rebase fails with trailing whitespace :

Combine commits with rebase :

Merge all my commits on top of commit [commit-hash]

git rebase --interactive [commit-hash]

in the interactive window, first line leave pick and change to s (Squash) all others save and quit (:wq)

Editor will popup again to provide a final name of the commit, you can edit/save/quit at your convenience (:wq)

remove merged commit

git rebase -i HEAD~1 --preserve-merges

reset the remote with local change

git push -f

reset your local with latest remote repo

git fetch
git reset origin/master --hard

Note --hard will also delete all local current changes (staged and committed)

combine multiple PRs

A PR is always submitted by another github user repo from a specific branch (main, feature_name, ...)

1 - add all the remote repo

git add otherrepo

2 - fetch their branches

git fetch otherrepo

3 - Create a new local repo and start merging the other repo branches

git checkout -b combine_PRs
git pull otherrepo branch_name

or keep cherry picking a commit from the fetched branch/repo

git cherry-pick 4d1709b01dc572356955b8f5633d90322746f5d1
git fetch branch
git cherry-pick 2b066d712a8e28d0c16f355e3b39d6fab3cc0819

eventually fix any conflict and add the file into the current working branch then continue

git add thefilewithconflictresolved
git cherry-pick --continue

Rename commit messages

clear repo with untracked changes

Ssh agent

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_25519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment