Git Notes

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Introduction


Git is a popular free and open-source distributed version control tool.

Basic commands


  • Initialize a git repo
1
git init
  • Add file to git repo
1
git add <paths>
  • Add all files
1
git add .
  • Undo add
1
git reset
  • Reset a single file in the index
1
git reset -- <file>
  • Undo last two commits
1
git reset --hard HEAD^
  • Reset to some commit
1
git reset --hard commit id
  • Restore file
1
git checkout -- <file>
  • Record changes to repo
1
git commit -m "msg"
  • Show the working tree status
1
git status
  • Show changes
1
git diff
  • Changes in some file in the working tree since last commit
1
git diff HEAD -- <file>
  • Show commit logs
1
git log
  • Pretty-print the contents of the commit logs in a given format
1
git log --pretty=oneline
  • Draw graph of the commit history
1
git log --graph
  • Show only partial prefix
1
git log --graph --pretty=oneline --abbrev-commit
  • Show last commit log
1
git log -1
  • Show reference logs
1
git reflog
  • Remove files from the working tree and from the index
1
git rm <file>
  • Show branches
1
git branch
  • Create a branch
1
git branch <branchname>
  • Switch to a branch
1
git checkout <name>
  • Create and switch to a branch
1
git checkout -b <name>
  • Create and switch to a branch and relate it to a remote repo branch
1
git checkout -b dev origin/dev
1
git checkout -b branch-name origin/branch-name
  • Delete a branch
1
git branch -d <name>
  • Force to delete a branch
1
git branch -D <name>
  • Relate local branch to remote branch
1
git branch --set-upstream branch-name origin/branch-name
  • Join different branches
1
git merge <name>
  • Merge without fast-forward
1
git merge --no-ff -m "msg" <name>
  • Stash the changes in a dirty working directory away
1
git stash
  • List current stashes
1
git stash list
  • Apply a stash and remove it from stash list
1
git stash pop
  • Apply a stash
1
git stash apply
  • Remove a stash
1
git stash drop
  • Show remote repo
1
git remote
  • Show remote url after name
1
git remote -v
  • Add remote repo
1
git remote add origin [email protected]:path/repo-name.git
  • Clone remote repo
1
git clone [email protected]:path/repo-name.git
  • Fetch and integrate
1
git pull
  • Update remote repo
1
git push origin master
  • Update and add tracking reference
1
git push -u origin master
  • Push tag
1
git push origin <tagname>
  • Push all tags
1
git push origin --tags
  • Delete remote tag
1
git push origin :refs/tags/<tagname>
  • Create tag
1
git tag <name>
  • List tags
1
git tag
  • Add tag to some commit
1
git tag <tagname> <commitid>
  • Create tag with message
1
git tag -a <tag> -m "msg" <commitid>
  • Delete tag
1
git tag -d <tagname>
  • Show tag info
1
git show <tagname>
  • set alisa name
1
git config --global alias.<aliasname> <name>

Reference and resources