I use Git every day, but I can’t remember many commands.
Generally speaking, for daily use, you only need to remember the 6 commands shown below. But to use it proficiently, I’m afraid you need to remember 60 to 100 commands.
Below is a list of commonly used Git commands that I compiled. The translations of several technical terms are as follows.
- Workspace: working directory
- Index / Stage: staging area
- Repository: repository area (or local repository)
- Remote: remote repository
1. Creating a new repository
# Create a new Git repository in the current directory $ git init # Create a new directory and initialize it as a Git repository $ git init [project-name] # Download a project and its entire code history $ git clone [url]
2. Configuration
Git’s configuration file is .gitconfig. It can be located in the user’s home directory (global configuration) or in the project directory (project configuration).
# Show the current Git configuration $ git config --list # Edit the Git configuration file $ git config -e [--global] # Set the user information for code commits $ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
3. Adding/Deleting Files
# Add specified files to the staging area $ git add [file1] [file2] ... # Add the specified directory to the staging area, including subdirectories $ git add [dir] # Add all files in the current directory to the staging area $ git add . # Confirmation will be required before adding each change # For multiple changes in the same file, this allows partial commits $ git add -p # Delete files from the working directory and stage the deletions $ git rm [file1] [file2] ... # Stop tracking the specified file, but keep it in the working directory $ git rm --cached [file] # Rename a file and stage the rename $ git mv [file-original] [file-renamed]
IV. Code Commit
# Commit the staging area to the repository $ git commit -m [message] # Commit the specified files in the staging area to the repository $ git commit [file1] [file2] ... -m [message] # Commit changes in the working directory since the last commit directly to the repository $ git commit -a # Show all diff information when committing $ git commit -v # Use a new commit to replace the previous commit # If there are no new code changes, this can be used to rewrite the previous commit message $ git commit --amend -m [message] # Redo the previous commit and include new changes from the specified files $ git commit --amend [file1] [file2] ...
V. Branches
# List all local branches $ git branch # List all remote branches $ git branch -r # List all local and remote branches $ git branch -a # Create a new branch, but stay on the current branch $ git branch [branch-name] # Create a new branch and switch to it $ git checkout -b [branch] # Create a new branch pointing to the specified commit $ git branch [branch] [commit] # Create a new branch and set up a tracking relationship with the specified remote branch $ git branch --track [branch] [remote-branch] # Switch to the specified branch and update the working directory $ git checkout [branch-name] # Switch to the previous branch $ git checkout - # Set up a tracking relationship between the existing branch and the specified remote branch $ git branch --set-upstream [branch] [remote-branch] # Merge the specified branch into the current branch $ git merge [branch] # Select a commit and merge it into the current branch $ git cherry-pick [commit] # Delete a branch $ git branch -d [branch-name] # Delete a remote branch $ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
VI. Tags
# List all tags $ git tag # Create a new tag on the current commit $ git tag [tag] # Create a new tag on the specified commit $ git tag [tag] [commit] # Delete a local tag $ git tag -d [tag] # Delete a remote tag $ git push origin :refs/tags/[tagName] # View tag information $ git show [tag] # Push the specified tag $ git push [remote] [tag] # Push all tags $ git push [remote] --tags # Create a new branch pointing to a certain tag $ git checkout -b [branch] [tag]
VII. View Information
# Show changed files $ git status # Show the version history of the current branch $ git log # Show the commit history, along with the files changed in each commit $ git log --stat # Search the commit history by keyword $ git log -S [keyword] # Show all changes after a certain commit, with each commit on a single line $ git log [tag] HEAD --pretty=format:%s # Show all changes after a certain commit whose "commit message" matches the search criteria $ git log [tag] HEAD --grep feature # Show the version history of a file, including renames $ git log --follow [file] $ git whatchanged [file] # Show every diff related to the specified file $ git log -p [file] # Show the last 5 commits $ git log -5 --pretty --oneline # Show all users who have made commits, sorted by number of commits $ git shortlog -sn # Show who modified the specified file and when $ git blame [file] # Show the differences between the staging area and the working directory $ git diff # Show the differences between the staging area and the previous commit $ git diff --cached [file] # Show the differences between the working directory and the latest commit on the current branch $ git diff HEAD # Show the differences between two commits $ git diff [first-branch]...[second-branch] # Show how many lines of code you wrote today $ git diff --shortstat "@{0 day ago}" # Show the metadata and content changes of a certain commit $ git show [commit] # Show the files changed in a certain commit $ git show --name-only [commit] # Show the content of a file at a certain commit $ git show [commit]:[filename] # Show the most recent commits on the current branch $ git reflog
8. Remote Synchronization
# Download all changes from the remote repository $ git fetch [remote] # Show all remote repositories $ git remote -v # Show information about a remote repository $ git remote show [remote] # Add a new remote repository and name it $ git remote add [shortname] [url] # Fetch changes from the remote repository and merge them into the local branch $ git pull [remote] [branch] # Upload the specified local branch to the remote repository $ git push [remote] [branch] # Force push the current branch to the remote repository, even if there are conflicts $ git push [remote] --force # Push all branches to the remote repository $ git push [remote] --all
9. Undo
# Restore the specified file from the staging area to the working directory $ git checkout [file] # Restore the specified file from a certain commit to the staging area and working directory $ git checkout [commit] [file] # Restore all files from the staging area to the working directory $ git checkout . # Reset the specified file in the staging area to match the last commit, leaving the working directory unchanged $ git reset [file] # Reset the staging area and working directory to match the last commit $ git reset --hard # Reset the current branch pointer to the specified commit, and reset the staging area while leaving the working directory unchanged $ git reset [commit] # Reset the HEAD of the current branch to the specified commit, and reset both the staging area and working directory to match it $ git reset --hard [commit] # Reset the current HEAD to the specified commit, while keeping the staging area and working directory unchanged $ git reset --keep [commit] # Create a new commit to undo the specified commit # All changes in the latter will be canceled out by the former and applied to the current branch $ git revert [commit] # Temporarily remove uncommitted changes and reapply them later $ git stash $ git stash pop
10. Others
# Create a distributable archive $ git archive