newly build
Create a new git version library. This version, configuration, etc. will be saved to the repository Git folder
# Initialize the current project $ git init # Create a new directory and initialize it as a Git code base $ git init [project-name] # Create an empty git repository in the specified directory. Running this command creates a directory that contains only Empty directory of git subdirectory. $ git init --bare <directory> # Download a project and its entire code history # This command is to copy a version library to another directory, and also copy all branches to the new version library. This allows you to commit to the remote branch in the new version library $ git clone [url]
to configure
Change settings. It can be the setting of version library, system or global
# Displays the current Git configuration $ git config --list # Edit Git profile $ git config -e [--global] # Output and set basic global variables $ git config --global user.email $ git config --global user.name $ git config --global user.email "MyEmail@gmail.com" $ git config --global user.name "My Name" # Define the author mailbox used by all submissions of the current user. $ git config --global alias.<alias-name> <git-command> # Create a shortcut (alias) for the Git command. $ git config --system core.editor <editor>
help
git has built-in very detailed explanation of the command, which can be consulted quickly
# Find available commands $ git help # Find all available commands $ git help -a # Find specific commands in the document # Git help < command > $ git help add $ git help commit $ git help init
state
Displays the difference between the index file (that is, the current workspace) and the submission pointed to by the current header pointer
# Show branches, untracked files, changes and other differences $ git status # Check the usage of other git status $ git help status
information
Get git information such as some files, some branches, a submission, etc
# Displays the commit history and the files that change each time a commit occurs $ git log --stat # Search submission history according to keywords $ git log -S [keyword] # All changes after a commit are displayed, and each commit occupies one line $ git log [tag] HEAD --pretty=format:%s # Display all changes after a commit, and its "submission description" must meet the search criteria $ git log [tag] HEAD --grep feature # Displays the version history of a file, including file renaming $ git log --follow [file] $ git whatchanged [file] # Displays each diff related to the specified file $ git log -p [file] # Show last 5 submissions $ git log -5 --pretty --oneline # Displays all submitted users, sorted by submission times $ git shortlog -sn # Displays who modified the specified file and when $ git blame [file] # Show differences between staging and workspace $ git diff # Displays the difference between the staging area and the previous commit $ git diff --cached [file] # Displays the difference between the workspace and the latest commit of the current branch $ git diff HEAD # Displays the difference between two submissions $ git diff [first-branch]...[second-branch] # Show how many lines of code you wrote today $ git diff --shortstat "@{0 day ago}" # Compare staging area and version library differences $ git diff --staged # Compare staging area and version library differences $ git diff --cached # Compare statistics only $ git diff --stat # Displays the metadata and content changes of a submission $ git show [commit] # Displays the files that have changed in a submission $ git show --name-only [commit] # Displays the contents of a file at the time of a submission $ git show [commit]:[filename] # Displays the most recent commits of the current branch $ git reflog # View remote branches $ git br -r # Create a new branch $ git br <new_branch> # View the last submission information of each branch $ git br -v # View branches that have been merged into the current branch $ git br --merged # View branches that have not been merged into the current branch $ git br --no-merged
add to
Add files to the current workspace. If you do not use git add to add files, they will not be added to subsequent submissions
# Add a file $ git add test.js # Add files in a subdirectory $ git add /path/to/file/test.js # regular expression $ git add ./*.js # Adds the specified file to the staging area $ git add [file1] [file2] ... # Adds 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 is required before adding each change # For multiple changes in the same file, it can be submitted in batches $ git add -p
delete
rm, in contrast to the add command above, removes a file from the workspace
# Remove HelloWorld js $ git rm HelloWorld.js # Remove files from subdirectories $ git rm /pather/to/the/file/HelloWorld.js # Delete the workspace file and put the deletion into the staging area $ git rm [file1] [file2] ... # Stops tracking the specified file, but the file remains in the workspace $ git rm --cached [file]
branch
To manage branches, you can add, delete, change, query and switch branches through the following commands
# View all branches and remote branches $ git branch -a # Create a new branch $ git branch [branch-name] # Rename branch # Git branch - M < old name > < new name > $ git branch -m [branch-name] [new-branch-name] # Introduction to editing branches $ git branch [branch-name] --edit-description # List all local branches $ git branch # List all remote branches $ git branch -r # Create a new branch, but still stay in the current branch $ git branch [branch-name] # Create a new branch and switch to it $ git checkout -b [branch] # Create a new branch and point to the specified commit $ git branch [branch] [commit] # Create a new branch and establish a tracking relationship with the specified remote branch $ git branch --track [branch] [remote-branch] # Switch to the specified branch and update the workspace $ git checkout [branch-name] # Switch to previous branch $ git checkout - # Establish a tracking relationship between an existing branch and a specified remote branch $ git branch --set-upstream [branch] [remote-branch] # Merge the specified branch to the current branch $ git merge [branch] # Select a commit to merge into the current branch $ git cherry-pick [commit] # Delete branch $ git branch -d [branch-name] # Delete remote branch $ git push origin --delete [branch-name] $ git branch -dr [remote/branch] # Switch to a branch $ git co <branch> # Create a new branch and switch to the past $ git co -b <new_branch> # Create new based on branch_ branch $ git co -b <new_branch> <branch> # A history submission record is checked out, but there is no branch information. Switching to other branches will automatically delete it $ git co $id # Check out a history submission record and create a branch $ git co $id -b <new_branch> # Delete a branch $ git br -d <branch> # Forcibly delete a branch (it is necessary to forcibly delete branches that have not been merged) $ git br -D <branch>
detection
Updates the current workspace to the workspace identified by the index or a specific workspace
# Check out a version library, which will be updated to the master branch by default $ git checkout # Check out a specific branch $ git checkout branchName # Create a new branch and switch to the past, which is equivalent to "git branch < name >; git checkout < name >" $ git checkout -b newBranch
Remote synchronization
Remote branch of remote synchronization
# Download all changes of remote warehouse $ git fetch [remote] # Show all remote warehouses $ git remote -v # Displays information about a remote warehouse $ git remote show [remote] # Add a new remote warehouse and name it $ git remote add [shortname] [url] # View remote server address and warehouse name $ git remote -v # Add remote warehouse address $ git remote add origin git@ github:xxx/xxx.git # Set remote warehouse address (used to modify remote warehouse address) $ git remote set-url origin git@ github.com:xxx/xxx.git # Delete remote warehouse $ git remote rm <repository> # Upload specified local branch to remote warehouse # Update the local branch to the master branch of the remote origin # Git push < remote > < Branch > # Git push is equivalent to git push origin master $ git push [remote] [branch] # Forcibly push the current branch to the remote warehouse, even if there is a conflict $ git push [remote] --force # Push all branches to remote warehouse $ git push [remote] --all
revoke
# Restore the specified files in the staging area to the workspace $ git checkout [file] # Restore the specified file of a commit to the staging area and workspace $ git checkout [commit] [file] # Restore all files in the staging area to the workspace $ git checkout . # Reset the specified file in the staging area, which is consistent with the last commit, but the workspace remains unchanged $ git reset [file] # Reset the staging area and workspace to be consistent with the last commit $ git reset --hard # Reset the pointer of the current branch to the specified commit, and reset the staging area at the same time, but the workspace remains unchanged $ git reset [commit] # Reset the HEAD of the current branch to the specified commit, and reset the staging area and workspace at the same time, which is consistent with the specified commit $ git reset --hard [commit] # Reset the current HEAD to the specified commit, but leave the staging area and workspace unchanged $ git reset --keep [commit] # Create a new commit to revoke the specified commit # All changes in the latter will be offset by the former and applied to the current branch $ git revert [commit] # Restore the last submitted state $ git revert HEAD # Temporarily remove uncommitted changes and move in later $ git stash $ git stash pop # Column all stash $ git stash list # Recover staged content $ git stash apply # Delete staging area $ git stash drop
commit
Save the changes of the current index as a new submission, which includes the changes and information made by the user
# Submit temporary storage area to warehouse area with submission information $ git commit -m [message] # Submit the specified files in the temporary storage area to the warehouse area $ git commit [file1] [file2] ... -m [message] # Submit the changes in the workspace since the last commit and go directly to the warehouse area $ git commit -a # Show all diff information when submitting $ git commit -v # Use a new commit instead of the last commit # If there are no new changes in the code, it is used to rewrite the submission information of the last commit $ git commit --amend -m [message] # Redo the last commit and include new changes to the specified file $ git commit --amend [file1] [file2] ...
diff
Displays the difference between the current workspace and the submitted
# Show differences between working directory and index $ git diff # Displays the difference between the index and the last submitted $ git diff --cached # Displays the difference between the working directory and the last submitted $ git diff HEAD
grep
You can quickly find it in the version library
Optional configuration:
# Thank Travis Jeffery for the following usage: # Show line numbers in search results $ git config --global grep.lineNumber true # Yes, the search results are more readable $ git config --global alias.g "grep --break --heading --line-number"
# Find variableName in all java $ git grep 'variableName' -- '*.java' # Search all rows containing "arrayListName" and "add" or "remove" $ git grep -e 'arrayListName' --and \( -e add -e remove \)
log
Show all submissions for this repository
# Show all submissions $ git log # Show some submitted information $ git log -n 10 # Show merge submissions only $ git log --merges # View the record of each submission of this file $ git log <file> # View diff of each detailed modification $ git log -p <file> # View the diff of the last two detailed modifications $ git log -p -2 #View submission statistics $ git log --stat
merge
Merge the external branches into your own
# Merge other branches into the current branch $ git merge branchName # Create a new merged submission when merging # Do not merge fast forward, which can generate a merge submission $ git merge --no-ff branchName
mv
Rename or move a file
# rename $ git mv test.js test2.js # move $ git mv test.js ./new/path/test.js # Rename the file and put the rename in the staging area $ git mv [file-original] [file-renamed] # Force rename or move # This file already exists and will be overwritten $ git mv -f myFile existingFile
tag
# List all tag s $ git tag # Create a new tag in the current commit $ git tag [tag] # Create a new tag and specify the commit $ git tag [tag] [commit] # Delete local tag $ git tag -d [tag] # Delete remote tag $ git push origin :refs/tags/[tagName] # View tag information $ git show [tag] # Submit the specified tag $ git push [remote] [tag] # Submit all tag s $ git push [remote] --tags # Create a new branch to point to a tag $ git checkout -b [branch] [tag]
pull
Merge from remote repository to current branch
# Update the version library from the master branch of the remote origin # Git pull < remote > < Branch > $ git pull origin master # Grab all branch updates from the remote warehouse and merge them locally. Do not fast forward the merge $ git pull --no-ff
ci
$ git ci <file> $ git ci . # Combine git add, git rm and git ci operations together $ git ci -a $ git ci -am "some comments" # Modify the last submission record $ git ci --amend
rebase (use with caution)
Apply all commit history on one branch to another. Do not use rebase on a remote branch that has been exposed
# Apply experimentBranch to master # git rebase <basebranch> <topicbranch> $ git rebase master experimentBranch
reset (use with caution)
Resets the current header pointer to a specific state. This allows you to undo merge, pull, commit, add, etc. This is a powerful command, but you must be aware of the consequences when using it
# Restore the staging area to the state of the last submission without changing the current working directory $ git reset # Restore the staging area to the state of the last submission and overwrite the current working directory $ git reset --hard # Restore the current branch to a submission without changing the current working directory # All changes still exist in the working directory $ git reset dha78as # Restore the current branch to a submission and overwrite the current working directory # And delete all uncommitted changes and all submissions after the specified submission $ git reset --hard dha78as
other
# Generate a compressed package for publishing $ git archive # patch up $ git apply ../sync.patch # Test whether the patch is successful $ git apply --check ../sync.patch # View Git version $ git --version
Recommended reading
An article that teaches you to learn Git
git simple command line tutorial