Git Command Line Interface#
Pro Git English Version: https://git-scm.com/book/en/v2
Basics#
git help <command>
: Get help information for git commandsgit init
: Create a new git repository, with the data stored in a directory named.git
git status
: Show the current repository statusgit add <filename>
: Add a file to the staging areagit commit
: Create a new commit- How to write good commit messages!
- Why write good commit messages
git log
: Show the commit historygit log --all --graph --decorate
: Visualize the commit history (directed acyclic graph)git diff <filename>
: Show the difference between the file and the staged versiongit diff <revision> <filename>
: Show the difference between two versions of a filegit checkout <revision>
: Update HEAD and the current branch
Branches and Merging#
git branch
: Show branchesgit branch <name>
: Create a branchgit checkout -b <name>
: Create a branch and switch to it- Equivalent to
git branch <name>; git checkout <name>
- Equivalent to
git merge <revision>
: Merge into the current branchgit mergetool
: Use a tool to handle merge conflictsgit rebase
: Rebase a series of patches to a new base
Remote Operations#
git remote
: List remotesgit remote add <name> <url>
: Add a remotegit push <remote> <local branch>:<remote branch>
: Transfer objects to a remote and update remote referencesgit branch --set-upstream-to=<remote>/<remote branch>
: Create an association between a local and remote branchgit fetch
: Get objects/refs from a remotegit pull
: Equivalent togit fetch; git merge
git clone
: Download a repository from a remote
Undoing#
git commit --amend
: Edit the contents or message of a commitgit reset HEAD <file>
: Unstage a filegit checkout -- <file>
: Discard changesgit restore
: Replaces git reset for many undo operations starting from git 2.32
Advanced Git Operations#
git config
: Git is a highly customizable toolgit clone --depth=1
: Shallow clone, excluding complete version historygit add -p
: Interactive staginggit rebase -i
: Interactive rebasegit blame
: View the person who last modified a linegit stash
: Temporarily remove modifications from the working directorygit bisect
: Search the commit history using binary search.gitignore
: Specifies intentionally untracked files gitignore documentation
Miscellaneous#
- Graphical User Interface: Git has many graphical user interface clients, but we prefer using the command line interface ourselves
- Shell Integration: Integrating Git status into your shell can be very convenient (zsh, bash). Frameworks like Oh My Zsh generally include this functionality
- Editor Integration: Similar to the previous point, integrating Git into your editor can be beneficial. fugitive.vim is a commonly used Vim plugin for Git integration
- Workflows: We have covered the data model and some basic commands, but we have not discussed some conventions when working on large projects (there are many different approaches)
- GitHub: Git is not the same as GitHub. On GitHub, you need to use a method called pull request to contribute code to other projects
- Other Git Providers: GitHub is not the only one. There are platforms like GitLab and BitBucket.
Resources#
- Pro Git, highly recommended! Learning the content of the first five chapters will teach you most of the tricks to use Git fluently, as you will understand Git's data model. The later chapters provide many interesting advanced topics. (Pro Git Chinese Version)
- Oh Shit, Git!?, a brief introduction on how to recover from Git mistakes
- Git for Computer Scientists, a brief introduction to Git's data model, with less pseudocode and more beautiful pictures compared to this article
- Git from the Bottom Up, a detailed introduction to Git's implementation details, not just limited to the data model. Curious students can take a look
- How to explain git in simple words
- Learn Git Branching, learn Git through a browser-based game
Exercises#
- If you have never used Git before, we recommend reading the first few chapters of Pro Git or completing tutorials like Learn Git Branching. Focus on Git commands and the data model.
- Fork the repository of this course website
- Visualize the version history and explore it
git log --all --graph --decorate
- Who was the last person to modify the
README.md
file? (Hint: Use thegit log
command with appropriate parameters)git log -1 README.md
- -x option: View the latest x commits or version information of a specific file
- What is the commit message when the
collections:
line in the_config.yml
file was last modified? (Hint: Usegit blame
andgit show
)git blame _config.yml | grep collections git show --pretty=format:"%s" a88b4eac | head -1 git log --pretty=format:"%s" a88b4eac -1
- Visualize the version history and explore it
- A common mistake when using Git is committing large files that should not be managed by Git or committing files with sensitive information. Try adding a file to the repository and committing it, then remove it from history (this article may help).
- First, commit some sensitive information
echo "password123">my_password git add . git commit -m "add password123 to file" git log HEAD
- Use
git filter-branch
to clean up the commit historygit filter-branch --force --index-filter\ 'git rm --cached --ignore-unmatch ./my_password' \ --prune-empty --tag-name-filter cat -- --all
- First, commit some sensitive information
- Clone a repository from GitHub, make some changes. What happens when you use
git stash
? What doesgit log --all --oneline
show? When would you use thegit stash pop
command? - Like other command-line tools, Git provides a configuration file called
~/.gitconfig
(or dotfile). Create an alias in~/.gitconfig
so that when you rungit graph
, you get the output ofgit log --all --graph --decorate --oneline
. - You can create global ignore rules by executing
git config --global core.excludesfile ~/.gitignore_global
and configuring your global gitignore file to automatically ignore temporary files from the system or editor, such as.DS_Store
. - Clone the repository of this course website from GitHub and look for any typos or areas for improvement. Make a pull request on GitHub.