About Git

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.

Since Git has been widely known, this article will not describe the Git tutorial in detail, only introduce some tools or learning materials.

Git tutorial: Git Book

Git GUI Client: TortoiseGit(Windows)

Git Common commands

Repository

  1. # Create a new Git repository
  2. $ git init
  3. # Initialize a Git repository for project
  4. $ git init [project-name]
  5. # Clone a repository into a directory
  6. $ git clone [url]

Configuration

  1. # Display git configuration of current repository
  2. $ git config --list
  3. # Edit git configuration file
  4. $ git config -e [--global]
  5. # Set git user.name and user.email
  6. $ git config [--global] user.name "[name]"
  7. $ git config [--global] user.email "[email address]"

Add/Remove File

  1. # Add file contents to the index
  2. $ git add [file1] [file2] ...
  3. # Add whole directory to the index
  4. $ git add [dir]
  5. # Add all files of current directory to the index
  6. $ git add .
  7. # Before adding each change, it will ask for confirmation
  8. # For multiple changes of the same file, it can be submitted in stages
  9. $ git add -p
  10. # Git will remove files from repository
  11. $ git rm [file1] [file2] ...
  12. # Git remove the file's contents from the index
  13. $ git rm --cached [file]
  14. # Git move or rename file, a directory, or a symlink
  15. $ git mv [file-original] [file-renamed]

Commit

  1. # Record changes to the repository
  2. $ git commit -m [message]
  3. # Record multiple files changes to the repository
  4. $ git commit [file1] [file2] ... -m [message]
  5. # Tell the command to automatically index files that have been modified and deleted, but new files you have not told Git about are not affected.
  6. $ git commit -a
  7. # Show unified diff between the HEAD and index file
  8. $ git commit -v
  9. # Replace the tip of the current branch by creating a new commit
  10. $ git commit --amend -m [message]
  11. # Redo last commit with selected files
  12. $ git commit --amend [file1] [file2] ...

Branch

  1. # List the local branch
  2. $ git branch
  3. # List the remote branch
  4. $ git branch -r
  5. # List the local and the remote branch
  6. $ git branch -a
  7. # Create new branch
  8. $ git branch [branch-name]
  9. # Create new branch and switch to new branch
  10. $ git checkout -b [branch]
  11. # Create new branch and point to this commit
  12. $ git branch [branch] [commit]
  13. # Create a new branch and establish a tracking with remote branch
  14. $ git branch --track [branch] [remote-branch]
  15. # Switch to branch
  16. $ git checkout [branch-name]
  17. # Switch to last branch
  18. $ git checkout -
  19. # Establish a tracking relationship between the local branch and the remote branch
  20. $ git branch --set-upstream [branch] [remote-branch]
  21. # Merge branch to current branch
  22. $ git merge [branch]
  23. # Select a commit and merge to main branch
  24. $ git cherry-pick [commit]
  25. # Delete local branch
  26. $ git branch -d [branch-name]
  27. # Delete remote branch
  28. $ git push origin --delete [branch-name]
  29. $ git branch -dr [remote/branch]

tags

  1. # List all tag
  2. $ git tag
  3. # Create a tag for current commit
  4. $ git tag [tag]
  5. # Create a new tag at the specified commit
  6. $ git tag [tag] [commit]
  7. # Delete the local tag
  8. $ git tag -d [tag]
  9. # Delete the remote tag
  10. $ git push origin :refs/tags/[tagName]
  11. # Show tag info
  12. $ git show [tag]
  13. # Push tag to remote branch
  14. $ git push [remote] [tag]
  15. # Push all tags to remote branch
  16. $ git push [remote] --tags
  17. # Create new branch and point to the tag
  18. $ git checkout -b [branch] [tag]

Status and Log

  1. # Show project changes and project info
  2. $ git status
  3. # Show git log of current branch
  4. $ git log
  5. # Show git commit records
  6. $ git log --stat
  7. # Show git commit records with keyword
  8. $ git log -S [keyword]
  9. # Show git commit records with format lines
  10. $ git log [tag] HEAD --pretty=format:%s
  11. # Show git commit records with matched search
  12. $ git log [tag] HEAD --grep feature
  13. # Show git commit records of file
  14. $ git log --follow [file]
  15. $ git whatchanged [file]
  16. # Show every commit diff of specified file
  17. $ git log -p [file]
  18. # Show latest 5 commits
  19. $ git log -5 --pretty --oneline
  20. # Show all submitted users, sorted by the number of submissions
  21. $ git shortlog -sn
  22. # Show who and when modified file
  23. $ git blame [file]
  24. # Show changes between latest commit and current branch
  25. $ git diff
  26. # Show the diff between latest commit and current branch
  27. $ git diff --cached [file]|
  28. # Show the diff between HEAD and current branch
  29. $ git diff HEAD
  30. # Show the diff between 2 commits
  31. $ git diff [first-branch]...[second-branch]
  32. # Output total number of modified files, as well as number of added and deleted lines.
  33. $ git diff --shortstat "@{0 day ago}"
  34. # Show change of a commit
  35. $ git show [commit]
  36. # Show the changed file name in a commit
  37. $ git show --name-only [commit]
  38. # Show the file content in a commit
  39. $ git show [commit]:[filename]
  40. # Show last commit
  41. $ git reflog

Remote Sync

  1. # Get all changed files from remote repository and merge with local files
  2. $ git fetch [remote]
  3. # List all remote repositories
  4. $ git remote -v
  5. # Show infomation of remote repository
  6. $ git remote show [remote]
  7. # Add a new remote repository and rename it
  8. $ git remote add [shortname] [url]
  9. # Get all changed files from remote repository and merge with local branch
  10. $ git pull [remote] [branch]
  11. # Push local changed files to remote repository
  12. $ git push [remote] [branch]
  13. # Push local changed files to remote repository, even if there are conflicts
  14. $ git push [remote] --force
  15. # Push all local changed files to remote repository
  16. $ git push [remote] --all

reset

  1. # Restore the specified files from index area to current branch
  2. $ git checkout [file]
  3. # Restore the specified file of a commit to current branch
  4. $ git checkout [commit] [file]
  5. # Restore all file of a commit to current branch
  6. $ git checkout .
  7. # Reset the specified file from index area to be consistent with the last commit
  8. $ git reset [file]
  9. # Reset files of index area to be consistent with the last commit
  10. $ git reset --hard
  11. # Reset the pointer of the current branch to the specified commit, and reset index area
  12. $ git reset [commit]
  13. # Reset the HEAD of the current branch to the specified commit, and reset index area and current branch, consistent with the specified commit
  14. $ git reset --hard [commit]
  15. # Reset the current HEAD to the specified commit, but keep index area and the current branch are unchanged
  16. $ git reset --keep [commit]
  17. # Create a new commit to revoke the specified commit
  18. # All changes in the latter will be offset by the former and applied to the current branch
  19. $ git revert [commit]
  20. # Temporarily remove uncommitted changes and move them in later
  21. $ git stash
  22. $ git stash pop

other

  1. # Generate a compressed package for publishing
  2. $ git archive