Git Basics
Great Resources
- Git Online Documentation
- Git Online Tutorial
- man git
- man giteveryday
Concepts
- Branch - line of development for a project
- Working tree - place where files are checked out and committed
- Staging area - stores information about what will go into your next commit
- Repository - place where source code snapshots and branches are stored
- Tag - named commit
For more details see here.
Command overview
Command | Description |
---|---|
Initialization | |
git init [directory] | Create local repository |
Branching | |
git checkout branch-name | Checkout branch branch-name |
git checkout -b branch-name | Create branch branch-name |
git branch -a | List local and remote branches |
git branch -r | List remote branches |
git branch | List local branches |
git branch branch-name | Create branch branch-name |
git branch -D branch-name | Delete branch branch-name |
git branch -m branch-name new-branch-name | Rename branch |
File manipulation | |
git add foo.py | Add file foo.py to the staging area |
git add files/ | Add directory files/ to the staging area |
git rm foo.py | Remove file from staging area and working directory |
git rm --cached foo.py | Remove file from staging area but keep it in working directory |
git reset HEAD foo.py | Remove file from staging area |
git mv foo.py bar.py | Rename file foo.py to bar.py |
git checkout -- file.py | Discard changes to file.py in working directory |
Information | |
git status | Show status about current branch |
git log | Show git log |
git log --graph | Show git log using the graph option |
git log --oneline | Show the git commit headlines only |
git rev-parse HEAD | HEAD~x |
git rev-parse --short HEAD | Show the pretty commit hash of HEAD |
git diff | Show git diff of current branch and master branch |
git diff branch1 branch2 | Show git diff of branch1 and branch2 |
git diff branch file | Compare contents of 'file' with branch 'brancha' |
Version control | |
git commit | Commit modified files in the staged area |
git commit -m 'Message' | Commit modified files in the staged area |
git commit -m 'Message' --signoff | Commit modified files in the staged area |
git commit --amend | Edit the commit message of HEAD |
git push | |
git push repo branch | Push branch to repository repo |
git push -f repo branch | Overwrite the branch on repository repo |
git push repo :branch | Delete branch on repository repo |
git push --delete repo branch | Delete branch on repository repo |
git fetch | |
git pull | |
git rebase | |
git remote | |
git remote add remote-name URL | Add a remote repository |
git remote remove remote-name | Remove remote repository |
git remote rename old-remote-name new-remote-name | Rename remote repository name |
Note: An alias for HEAD is @. So, @~2 is the same as HEAD~2.