Cheatsheet of Git Commands


Created on: 4/14/19
Last update: 5/12/19


Remove last commit

Withdraw the last commit:

git rebase -i HEAD~2


Switch between branches

List all branches:

git branch -a

Switch to the target branch:

git checkout <BRANCH>

Create new branch

Create a branch on local host and switch to it:

git pull
git checkout -b <NEW_BRANCH>

Push the branch to remote (-u means set upstream along with the first git push as follows):

# solution 1
git push -u origin <NEW_BRANCH>

# solution 2
git branch --set-upstream <NEW_BRANCH>
git push origin <NEW_BRANCH>

How to clone all remote branches in Git?
What does ‘–set-upstream’ do?

Create Github Pages branch

git checkout --orphan gh-pages
git rm -rf .
git add .
git commit -m 'gh-pages init'
git push origin gh-pages

How to create gh-pages branch
Initialize gh-pages branch

Delete branch

Delete a local branch (-D means force delete):

git branch -d [BRANCH]
git branch -D [BRANCH]

Delete a remote GIT branch:

git push origin :[BRANCH]

Delete a local and a remote GIT branch.