Give yourself some time to remember these 200 Git commands

Give yourself some time to remember these 200 Git commands

New

  • Create a new git repository. The configuration, storage and other information of this repository will be saved in the .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 will create an empty directory named directory containing only the .git subdirectory. $ git init --bare <directory> # Download a project and its entire code history # This command is to copy a repository to another directory, and at the same time copy all branches to the new repository. So that you can submit to a remote branch in the new repository $ git clone [url] Copy the code

Configuration

  • Change the settings. It can be the settings of the version library, system or global
# Display the current Git configuration $ git config --list # Edit Git configuration file $ 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> Copy code

help

  • Git has built-in very detailed explanations of commands, which can be used for quick reference
# Find available commands $ git help # Find all available commands $ git help -a # Find a specific command in the document # git help <command> $ git help add $ git help commit Git $ Help the init copy the code

status

  • Show the difference between the index file (that is, the current workspace) and the commit pointed to by the current head pointer
# Show branches, untracked files, changes and other differences $ git status # See the other git status of usage $ git Help Status Copy the code

information

  • Get some files, some branches, some commits and other git information
# Display the commit history and the files that have changed each time $ git log - stat # Search the submission history, according to the keyword $ git log -S [keyword] # Display all changes after a certain commit, each commit occupies a line $ git log [tag] HEAD --pretty=format:%s # Display all changes after a commit, and its "commit description" must meet the search criteria $ git log [tag] HEAD --grep feature # Display the version history of a file, including file renaming $ git log --follow [file] $ git whatchanged [file] # Display every diff related to the specified file $ git log -p [file] # Display the last 5 commits $ git log -5 --pretty --oneline # Display all submitted users, sorted by the number of submissions $ git shortlog -sn # Show who modified the specified file and when $ git blame [file] # Display the difference between the temporary storage area and the work area $ git diff # Display the difference between the temporary storage area and the previous commit $ git diff --cached [file] # Display the difference between the workspace and the latest commit of the current branch $ git diff HEAD # Show the difference between the two submissions $ git diff [first-branch]...[second-branch] # Show how many lines of code you wrote today $ git diff --shortstat "@{0 day ago}" # Compare the difference between the staging area and the version library $ git diff --staged # Compare the difference between the staging area and the version library $ git diff --cached # Only compare statistics $ git diff - stat # Display the metadata and content changes of a certain submission $ git show [commit] # Display the files that have changed in a certain submission $ git show --name-only [commit] # Display the contents of a file at the time of submission $ git show [commit]:[filename] # Display the last few commits of the current branch $ git reflog # View remote branch $ git br -r # Create new branch $ git br <new_branch> # View the last commit information of each branch $ git br -v # View the branches that have been merged into the current branch $ git br --merged # View the branches that have not been merged into the current branch $ git br --no-merged Copy code

Add to

  • Add files to the current workspace. If you don't use
    git add
    Add the files, then these files will not be added to subsequent submissions
# Add a file $ git add test.js # Add a file in a subdirectory $ git add/path/to/file/test.js # Support regular expressions $ git add ./*.js # Add the specified file to the temporary storage area $ git add [file1] [file2] ... # Add the specified directory to the temporary storage area, including subdirectories $ git add [dir] # Add all files in the current directory to the temporary storage area $ git add. # Add each change before, will ask for confirmation # For multiple changes of the same file, it can be submitted in stages $ git add -p Copy code

delete

  • rm is the opposite of the add command above, removes a file from the workspace
# Remove HelloWorld.js $ git rm HelloWorld.js # Remove files in subdirectories $ git rm/pather/to/the/file/HelloWorld.js # Delete the work area file, and put this deletion into the temporary storage area $ git rm [file1] [file2] ... # Stop tracking the specified file, but the file will remain in the workspace $ git rm --cached [file] Copy code

Branch

  • Manage branches, you can add, delete, change, check, switch, etc. through the following commands
  • Branch (similar to a folder)
# 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] # Edit the introduction of the branch $ 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 this branch $ 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 the previous branch $ git checkout- # Establish a tracking relationship between the existing branch and the specified remote branch $ git branch --set-upstream [branch] [remote-branch] # Merge the specified branch to the current branch $ git merge [branch] # Choose a commit and 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 it $ git co -b <new_branch> # Create a new new_branch based on branch $ git co -b <new_branch> <branch> # Checkout a certain historical commit record, but there is no branch information, switching to another branch will automatically delete $ git co $id # Checkout a certain historical commit record and create a branch $ git co $id -b <new_branch> # Delete a branch $ git br -d <branch> # Forcibly delete a branch (required to be forced when a branch that has not been merged is deleted) $ git br -D <branch> Copy code

Check out

  • Update the current workspace to the one identified by the index or a specific workspace
  • Local (switch) >> Remote origin (check out and switch) >> None (cannot be used to create new branches)
# Check out a repository, which will be updated to the master branch by default $ git checkout # Check out to a specific branch $ git checkout branchName # Create a new branch and switch to it, which is equivalent to "git branch <name>; git checkout <name>" $ git checkout -b newBranch Copy code

Remote sync

  • Remote branch for remote synchronization
# Download all changes in the remote warehouse $ git fetch [remote] # Show all remote warehouses $ git remote -v # Display the information of a remote warehouse $ git remote show [remote] # Add a new remote warehouse and name it $ git remote add [shortname] [url] # View the remote server address and warehouse name $ git remote -v # Add remote warehouse address $ git remote add origin git@ github:xxx/xxx.git # Set the remote warehouse address (used to modify the remote warehouse address) $ git remote set-url origin git@ github.com:xxx/xxx.git # Delete remote warehouse $ git remote rm <repository> # Upload the local designated branch to the corresponding branch of the remote warehouse git push <remote host name> <local branch name>:<remote branch name> # The local branch name is the same as the remote branch name, you can omit the colon: # git push <remote> <branch> $ git push [remote] [branch] # Update the current local branch to the corresponding branch of the remote origin $ git push # currently in the local master branch # git push is equivalent to git push origin master # Forcibly push the current branch to the remote warehouse, even if there is a conflict $ git push [remote] --force # Push all branches to the remote warehouse $ git push [remote] --all Copy code

Revoke

# Restore the specified file in the temporary storage area to the work area $ git checkout [file] # Restore the specified file of a commit to the temporary storage area and the work area $ git checkout [commit] [file] # Restore all files in the temporary storage area to the work area $ git checkout. # Reset the specified file in the temporary storage area, which is consistent with the last commit, but the work area remains unchanged $ git reset [file] # Reset the temporary storage area and the work area to be consistent with the last commit $ git reset --hard # Reset the pointer of the current branch to the specified commit, and reset the temporary storage area at the same time, but the work area remains unchanged $ git reset [commit] # Reset the HEAD of the current branch to the specified commit, and reset the temporary storage area and work area at the same time, consistent with the specified commit $ git reset --hard [commit] # Reset the current HEAD to the specified commit, but keep the temporary storage area and work area unchanged $ git reset --keep [commit] # Create a new commit to undo 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 state of the last submission $ git revert HEAD # Temporarily remove uncommitted changes and move them in later $ git stash $ git stash pop # List all stash $ git stash list # Recover temporary content $ git stash apply # Delete staging area $ git stash drop Copy code

commit

  • Save the current index changes as a new submission, this submission includes the changes and information made by the user
# Submit the temporary storage area to the warehouse area with submission information $ git commit -m [message] # Submit the designated files in the temporary storage area to the warehouse area $ git commit [file1] [file2] ... -m [message] # Submit the changes in the work area since the last commit, directly to the warehouse area $ git commit -a # Display all diff information when submitting $ git commit -v # Use a new commit to replace the previous commit # If there is no new change in the code, it is used to rewrite the commit information of the last commit $ git commit --amend -m [message] # Redo the last commit and include the new changes in the specified file $ git commit --amend [file1] [file2] ... Copy code

diff

  • Show the difference between the current workspace and the submission
# Show the difference between the working directory and the index $ git diff # Display the difference between the index and the most recent submission $ git diff --cached # Show the difference between the working directory and the most recent submission $ git diff HEAD Copy code

grep

  • You can quickly find it in the repository
  • Optional:
# Thanks to Travis Jeffery for the following usage: # Display the line number in the search results $ git config --global grep.lineNumber true # 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 contains "arrayListName" and, "add" or "remove" all rows $ git grep -e 'arrayListName' --and/(-e -e the Remove the Add/) Copy the code

log

  • Show all commits in this repository
# Show all commits $ git log # Display certain submission information $ git log -n 10 # Only display merge commits $ git log --merges # View the record of each submission of the file $ git log <file> # View the diff of each detailed modification $ git log -p <file> # View the diff of the last two detailed changes $ git log -p -2 # View statistics submitted $ git log - STAT copy the code

merge

  • Merge is to merge external commits into your own branch
# Merge other branches into the current branch $ git merge branchName # Create a new merged submission when merging # Don t merge with Fast-Foward, so you can generate merge submissions $ git merge --no-ff branchName Copy code

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 this rename into the temporary storage area $ git mv [file-original] [file-renamed] # renaming or moving # This file already exists and will be overwritten $ git mv -f myFile existingFile Copy code

tag

# List all tags $ git tag # Create a new tag in the current commit $ git tag [tag] # Create a new tag at the specified 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 tags $ git push [remote] --tags # Create a new branch, pointing to a tag $ git checkout -b [branch] [tag] Copy code

pull

  • Merge from the remote repository to the current branch
# Update the repository from the master branch of the remote origin # git pull <remote> <branch> $ git pull origin master # Grab all branch updates of the remote warehouse and merge them locally, don t fast-forward and merge $ git pull --no-ff Copy code

ci

$ git ci <file> $ git ci. # Combine git add, git rm and git ci and other operations together $ git ci -a $ git ci -am "some comments" # Modify the last commit record $ git ci --amend Copy code

rebase (use with caution)

  • Apply all the commit history on one branch to another branch. Don't use rebase on a remote branch that is already public.
# Apply experimentBranch to the master # git rebase <basebranch> <topicbranch> $ git rebase master experimentBranch Copy code

reset (use with caution)

  • Reset the current head pointer to a specific state. This allows you to cancel merge, pull, commits, add, etc. This is a very powerful command, but you must be aware of the consequences when using it.
# Restore the staging area to the state when it was submitted last time, without changing the current working directory $ git reset # Restore the staging area to the state when it was submitted last time, overwriting the current working directory $ git reset --hard # Restore the current branch to a commit without changing the current working directory # All changes in the working directory still exist $ git reset dha78as # Restore the current branch to a commit, overwrite the current working directory # and delete all uncommitted changes and all commits after the specified commit $ git reset --hard dha78as Copy code

other

# Generate a compressed package for publishing $ git archive # Patch $ git apply ../sync.patch # Test whether the patch can be successful $ git apply --check ../sync.patch # View Git version $ git --version Copy code

If this article is helpful to you, leave one

like
Chant~