In git, Common commands are learned through actual operations.
Basic 6 commands
The following six commands are commonly used, but there may be hundreds of commands in detail.
# Create a Git code library [common] $ git init # create a directory in the current directory and initialize it as a Git code library, for example, $ git init [project-name]
Before initialization:
# Add a specified file to the temporary storage zone $ git add [file1] [file2]... # add a specified directory to the temporary storage area, including the subdirectory $ git add [dir] # add all files in the current directory to the temporary storage area [common] $ git add.
# Submit the temporary storage area to the warehouse area [common] $ git commit-m [message] # submit the specified file of the temporary storage area to the warehouse area $ git commit [file1] [file2]... -m [message] # submit the changes in the workspace since the previous commit, go directly to the warehouse $ git commit-a # Show All diff information when submitting $ git commit-v # use a new commit instead of the previous commit # If the Code does not change, it is used to rewrite the commit information of the previous commit $ git commit -- amend-m [message] # redo the previous commit, and includes new changes to the specified file $ git commit -- amend [file1] [file2]...
# Upload a local specified Branch to a remote repository, for example, git push origin develop [common] # If the current branch has a tracing relationship with multiple hosts, you can use the-u option to specify a default host, in this way, you can use git push $ git push [remote] [branch] # To forcibly push the current branch to a remote repository without adding any parameters, use $ git push [remote] With Caution -- force # push all branches to a remote repository $ git push [remote] -- all
Add a remote repository address before pushing
# Download a project and its entire code history in the current directory, such as git clone http: // 172.18.21.106/n. lu/GitFlowDemo. git $ git clone [url] [common] # specify a directory, for example, $ git clone [url] [local directory name]
To simulate this situation, delete the local workspace and then clone it.
# Retrieve remote repository changes and merge them with local branches $ git pull [remote] [branch]
# Creating a new branch, but still staying in the current branch $ git branch [branch-name] # creating a new branch, switch to the branch $ git checkout-B [branch] # create a new branch based on a branch, switch to this branch [common] $ git checkout-B [branch] [origin branch]
Next, I will create the development master branch develop from the master, and then create two functional branches feature_v2.0 and feature_v2.1 from the develop, as shown in.
# Merge a specified branch to the current branch. First, switch the branch $ git merge [branch] # merge the specified branch to the current branch. Do not merge fast foword, first, pay attention to switching the branch [common] $ git merge -- no-ff [branch]
For example, we first make some changes on the feature branch feature_v2.0, and then merge them to the develop.
# Delete branch $ git branch-d [branch-name] # delete remote branch $ git push origin -- delete [branch-name] $ git branch-dr [remote/branch]
After the feature branch feature_v2.0 is developed and merged to develop, we usually pull the corresponding release branch based on the develop branch. At this time, the lifecycle of the feature branch feature_v2.0 is complete and can be deleted, for example.
# Display the current Git configuration $ git config -- list # edit the Git configuration file $ git config-e [-- global] # Set the user information when code is submitted $ git config [-- global] user. name "[name]" $ git config [-- global] user. email "[email address]"
File Path: C: \ Users \ n. lu \. gitconfig. config
2. Labels
When the master has been submitted, You need to tag the version of the current master.
Git command:
# List all tags [commonly used] $ git tag # create a tag at the current commit [commonly used] $ git tag [tag] # create a tag at 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] # submit specified tag $ git push [remote] [tag] # submit all tags [common] $ git push [remote] -- tags # create a branch, point to a tag $ git checkout-B [branch] [tag]
3. View information
Git command:
# Display the changed File $ git status # display the version history of the current branch $ git log # display the commit history, and the file $ git log -- stat # That changes each time a commit is changed shows all the changes after a commit. Each commit occupies a line $ git log [tag] HEAD -- pretty = format: % s # displays all changes after a commit. Its "Submit description" must meet the search criteria $ git log [tag] HEAD -- grep feature # displays the version history of a file, including renaming a file $ git log -- follow [file] $ git whatchanged [file] # display each diff related to a specified file $ git log-p [file] # display what the specified file is when did a person modify $ git blame [file] # display the differences between the temporary storage zone and the workspace $ git diff # display the differences between the temporary storage zone 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 # display the difference between the two commits $ git diff [first-branch]... [second-branch] # display the metadata and content changes of a certain commit $ git show [commit] # display the file changed by a certain commit $ git show -- name-only [commit] # When a submission is displayed, content of a file $ git show [commit]: [filename] # displays the latest commits of the current branch $ git reflog
4. Undo the operation
Git command:
# Restore the specified file in the temporary storage area to the work zone $ git checkout [file] # restore the specified file of a commit to the work zone $ git checkout [commit] [file] # restore all of the previous commit file to the workspace $ git checkout. # reset the specified file in the temporary storage area, which is consistent with the previous commit, but the workspace remains unchanged $ git reset [file] # reset the temporary storage area and workspace, consistent with the previous commit $ git reset -- hard # reset the pointer of the current branch to specify the commit and reset the temporary store, but the workspace remains unchanged [commonly used] $ git reset [commit] # reset the HEAD of the current branch to specify the commit, and reset the temporary storage area and workspace 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 workspace unchanged $ git reset -- keep [commit] # create a new commit, all changes used to cancel a specified commit # the latter will be offset by the former and applied to the current branch $ git revert [commit]
References
1, Ruan Yifeng's list of common Git commands: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
2, vs with git: http://www.cnblogs.com/mcgrady/p/4701259.html