Git Series 4: advanced features of Git
Tip: In our git configuration and Usage Guide for windows, we will introduce how to use the $ git go command. In fact, this is not a native command of git. It is a custom alias (alias ), it is composed of four commands: $ git add, $ git commit, $ git push, and $ git pull. After you become familiar with it, you can directly use these native commands or customize your own Alias.
Add
Add a new file to the GIT code repository Index
$ git add filename
MV
Move or rename a file
$ git mv old-filename new-filename
Rm
Delete files from the working directory and git code Index
$ git rm filename
Status
View the code status of the current working directory, including the addition, modification, and deletion since the last submission.
$ git status
Diff
View the details of local code changes since the last submission
$ git diff
Commit
Submit the modified Code (only submitted to the local code library and not pushed to the server)
$ Git commit-am 'modify description'
If the submitted "modification description" is not well written, enter the following command to adjust it:
$ git commit --amend
Push
Pushes the local commit that has been pushed since the last time to the server.
Based on our actual situation, we should write as follows:
$ git push origin master:your-id
Here, the master is the local branch name, And the your-Id field is your id on the server. The server version library will have the branch named your ID.
Pull
Pull the code pushed to the server to your machine.
$ git pull
Log
View modification records, including author, time, and description
$ git log
Show
Display specific code changes
Display the last modified commit content:
$ git show
Display the modified content of the specified commit:
Tip: In the GIT log command, each commit has a long string, that is, the commid ID, which is the first five or six digits.
$ git show commit-id
Branch
Branch Management
List all branches (the current branch has a "*" before it ):
$ git branch
New Branch:
$ Git branch new branch name
Delete Branch:
$ Git branch-D name of the branch to be deleted
Note !] Do not write '-d' as'-d', which is dangerous!
- -D: required: all modifications to the deleted Branch have been merged to the current branch;
- -D: delete directly. unmerged code will be discarded!
Checkout ):
$ git checkout file-name
Switch to another branch for development:
$ git checkout branch-name
Note !] This command may be accompanied by a large number of file additions, deletions, and modifications. In Windows, changes to occupied files may be rejected, causing serious problems in the version library. If you want to do this, you 'd better log out first for security reasons.
Merge
Merge the specified branch to the current Branch:
$ git merge branch-name
Revert
Restore submitted changes. (If you have already submitted changes, you can regret them ~)
Restore the last submitted changes:
$ git revert HEAD
Restore the modification of a specified version:
$ git revert commit-id
Stash
Save uncommitted changes for temporary storage, and then clear all changes to make them the same as when they are not modified.
If you are developing function A, you need to develop function B immediately. The Code of A is being changed to half. You do not want to submit it immediately. Now ...... Please call Stash ~.
It will make all your uncommitted changes disappear instantly:
$ git stash
It will make the changes that have just disappeared and return instantly:
$ git stash pop
Tip: The above commands have more parameters, and some git commands are not described here. However, this is enough for you to use git, and you will feel that git is an artifact! :-)
[Tip] '$ git help' and '$ git HELP command name' will help you selflessly when you need it. :-)
Appendix: git push failure Solution
Assume that the operation is performed:
1. modify the code 2. Git commit3. git push
Push failed (error prompt :! [Rejected] Master-> master (non-fast-forward ))
Solution:
$ git pull
If successful, then:
$ git push origin master:your-id
Complete.
If the error message is "Conflict (content): Merge conflict in file name:
Conflicting files have code blocks similar to the following:
<Head the code you modified ============== code modified by others >>> commit ID of others'
Consider your and others' modifications to the Code, update the code into appropriate content, delete the <, =,> three line marking symbols, and save the file.
$ git commit -am "resolve conflict"$ git push origin master:your-id
For more details, read the note about fast-forwards section of $ git push-help.