Cheatsheet of Git Commands

 

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

Commit

Remove last commit

Withdraw the last commit:

git rebase -i HEAD~2

Branch

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>

Reference
How to clone all remote branches in Git? https://stackoverflow.com/questions/67699/how-to-clone-all-remote-branches-in-git
What does ‘–set-upstream’ do? https://stackoverflow.com/questions/18031946/what-does-set-upstream-do

Create Github Pages branch

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

Reference
How to create gh-pages branch https://coderwall.com/p/-bcoua/how-to-create-gh-pages-branch
Initialize gh-pages branch https://gist.github.com/skratchdot/1ef433c666066389201a

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]

Reference
Delete a local and a remote GIT branch. https://koukia.ca/delete-a-local-and-a-remote-git-branch-61df0b10d323