Git Common Commands Cheat Sheet (Part 3)

Machine-translated (anthropic, claude-haiku-5-5) from the Chinese original. Read the original in 中文

git init # Initialize a local Git repository (create a new repository)

git config --global user.name "xxx" # Configure user name

git config --global user.email "[email protected]" # Configure email

git config --global color.ui true # Automatically colorize output of commands such as git status

git config --global color.status auto

git config --global color.diff auto

git config --global color.branch auto

git config --global color.interactive auto

git clone git+ssh://[email protected]/VT.git # Clone a remote repository

git status # Check the current status (whether there are modifications)

git add xyz # Add file xyz to the index

git add . # Add all modified files in the current subdirectory to the index

git commit -m 'xxx' # Commit

git commit --amend -m 'xxx' # Amend the previous commit (for repeated revisions)

git commit -am 'xxx' # Combine add and commit into one step

git rm xxx # Remove a file from the index

git rm -r * # Recursive removal

git log # Show commit log

git log -1 # Show one-line log; -n shows n lines

git log -5

git log --stat # Show commit log along with the changed files

git log -p -m

git show dfb02e6e4f2f7b573337763e5c0013802e392818 # Show the detailed content of a commit

git show dfb02 # The first few characters of the commit ID can be used

git show HEAD # Show the log of HEAD

git show HEAD^ # Show the log of HEAD's parent (previous version); ^^ for the two previous versions, ^5 for the previous five versions

git tag # Show existing tags

git tag -a v2.0 -m 'xxx' # Add tag v2.0

git show v2.0 # Show the log and detailed content of v2.0

git log v2.0 # Show the log of v2.0

git diff # Show all changes not yet added to the index

git diff --cached # Show all changes added to the index but not yet committed

git diff HEAD^ # Compare with the previous version

git diff HEAD -- ./lib # Compare the lib directory with the HEAD version

git diff origin/master..master # Compare what exists on remote branch master but not on local branch master

git diff origin/master..master --stat # Show only the names of differing files, not the specific content

git remote add origin git+ssh://[email protected]/VT.git # Add a remote definition (used for push/pull/fetch)

git branch # Show local branches

git branch --contains 50089 # Show branches that contain commit 50089

git branch -a # Show all branches

git branch -r # Show all original branches

git branch --merged # Show all branches already merged into the current branch

git branch --no-merged # Show all branches not yet merged into the current branch

git branch -m master master_copy # Rename a local branch

git checkout -b master_copy # Create new branch master_copy from the current branch and check it out

git checkout -b master master_copy # Full version of the above

git checkout features/performance # Check out the existing features/performance branch

git checkout --track xxx/xxx # Check out remote branch hotfixes/BJVEP933 and create a local tracking branch

git checkout v2.0 # Check out version v2.0

git checkout -b devel origin/develop # Create new local branch devel from remote branch develop and check it out

git checkout -- README # Check out the README file from the HEAD version (can be used to revert mistaken edits)

git merge origin/master # Merge remote master branch into the current branch

git cherry-pick ff44785404a8e # Merge the changes from commit ff44785404a8e

git push origin master # Push the current branch to remote master branch

git push origin :xxx/xxx # Delete branch xxx/xxx from the remote repository

git push --tags # Push all tags to the remote repository

git fetch # Fetch all remote branches (does not update local branches; a merge is still needed)

git fetch --prune # Fetch all original branches and prune branches already deleted on the server

git pull origin master # Fetch remote branch master and merge it into the current branch

git mv README README2 # Rename file README to README2

git reset --hard HEAD # Reset the current version to HEAD (usually used to roll back after a failed merge)

git rebase

git branch -d xxx/xxx # Delete branch xxx/xxx (its changes have been merged into another branch)

git branch -D hotfixes/BJVEP933 # Force-delete branch hotfixes/BJVEP933

git ls-files # List the files included in the git index

git show-branch # Graph the history of the current branch

git show-branch --all # Graph the history of all branches

git whatchanged # Show the file changes corresponding to the commit history

git revert dfb02e6e4f2f7b573337763e5c0013802e392818 # Revert commit dfb02e6e4f2f7b573337763e5c0013802e392818

git ls-tree HEAD # Internal command: show a git object

git rev-parse v2.0 # Internal command: show the SHA1 hash corresponding to a ref

git reflog # Show all commits, including orphaned nodes

git show HEAD@{5}

git show master@{yesterday} # Show the state of the master branch as of yesterday

git log --pretty=format:'%h %s' --graph # Graph the commit log

git show HEAD~3

git show -s --pretty=raw 2be7fcb476

git stash # Stash current changes, marking all of them as HEAD状态

git stash list # List all stashes

git stash show -p stash@{0} # Refer to the first stash

git stash apply stash@{0} # Apply the first stash

Comments