Git Cheat Sheet

20 August 2019 Link



Terminology

  • HEAD is not the latest revision, it's the current revision. Usually, it's the latest revision of the current branch, but it doesn't have to be.
  • master is a name commonly given to the main branch, but it could be called anything else (or there could be no main branch).
  • origin is a name commonly given to the main remote. remote is another repository that you can pull from and push to. Usually it's on some server, like github.

Good GUI Software

  • Git Extensions - Very good for bash and windows explorer integration
  • SourceTree - Excellent GUI to manage the whole repository and look at it visually. Better visual representation by default then git extensions since gives the exact date and time.

Updating a submodule

  • Open a git bash at the sub-module directory then type:
cd submodule_name
git checkout master && git pull
cd ..
git add submodule_name
git commit -m "updating submodule to latest"
git push

Informational Commands

  • Repository Status
git status
  • Commit History
git log
  • Get current branch name
git branch

Checkin Commands

  • Push code to a remote at a particular branch
git push -u <remote name> <branch name>
-u option points your current local branch to the remote branch named
  • Stage files
    • Stages all
git add -A
    • Stages new and modified, without deleted
git add . 
    • stages modified and deleted, without new
git add -u 
  • Commit
git commit -m "Message"
  • Aborting a merge
git merge --abort
  • Discard local changes
git reset --hard
  • Stash Local changes and drop them
git stash save --keep-index (--include-untracked)
git stash drop

Checkout

  • Fetch all information about new branches etc. from remote
git fetch <remote name>
  • Pull the branch data from remote
git pull <remote name> <branch name>

Branching

  • Switch to existing branch
git checkout <branch name>
  • Switch to new branch on the current workspace i.e. branch off
git checkout -b <new branch name>
  • Push current branch to remote and track it
git push -u origin <branch name>

Remotes

  • List Remotes
git remote -v
  • Change Remote URL
git remote set-url <remote name> <url>
  • Change Remote name
git remote rename <old name> <new name>
  • Remove a remote
git remote rm <remote name>
  • Add a remote
git remote add <remote name> <url>

Tutorial and Help


Reference