Prior to using GIT, SVN version management was used, and the biggest difference with SVN is that git has two warehouses, one is a local repository, one is a shared repository on a server, and a local repository is unique to every developer, even if commit commits to the local repository only. This is just one of the advantages of Git's popularity, and the version management tools developed by Linux authors are very grounded, and they're a big spot for popularity. What git commands are common in daily development work, with so many useless words being rolled out? Here are a few common scenarios for commonly used commands, which are not covered in this article for detailed use.
1. Git command aliases
Git has been used to know that some of the command name of Git is a bit long, each time in the use of CHANGE commands to enter these, especially some command statements; The angry long string of strings of text is very laborious, the developer is also very uncomfortable, then our Git alias function can be the ink debut. Here are some of my favorite commands for alias configuration:
Command |
Alias |
Commit |
Ci |
Checkout |
Co |
Status |
St |
Merge Origin/master |
Mom |
Note: The above git command aliases are set in the computer User directory. gitconfig files are globally set so that all GIT branches can use the alias of the setting. Of course, depending on the individual preferences of different developers, can have their own alias settings;
2. Git checkout
This command is used to check out a branch, commit, or a corresponding branch, committed file. Depending on the parameters to check out the different content, then its common scenario is as follows:
- git checkout . Overwrite the contents of the current workspace with staging area content, then you can undo local branch changes by staging area content
In some cases, if you want to do a test in the current branch, you modify multiple files of the current branch, and then undo the changes to the current branch after testing, you can use Git checkout. Command
Note: Use this command to be aware that changes to all files in the local branch will be revoked
- git checkout-b newbranchname origin/master Create a new branch based on master and switch to the new branch
Avoiding Git branch It takes a step more to complete this feature:
Git branch newbranchname origin/master
git checkout Newbranchname
- git checkout- switch to the branch before switching to the current branch
Use scenario: Develop a local to create multiple branches to complete different requirements, it may be necessary to switch between different branches, such as in the development of a branch, the B branch of the BUG,QA to let fixed, then switch to the B branch fart fart repair bug, after the end to switch to a branch, You can now use the shortcut command
- git checkout commitid/branchname--Path replaces the local file with the specified commit ID, or a file of the branch, to undo the content change of the file
Usage scenario: For some reason, in a multitude of file changes, only the content change of a specified file is revoked
3. Git stash
This command is used to stage changes to a local file.
One day, the developer in a branch to develop a demand, suddenly this self-happy development way is interrupted, a test branch has a bug needs to be repaired immediately, wipe, the local code has not been submitted, but do not want to because of the temporary submission of local code to describe how the temporary submission of the description. You can then use Git stash to access locally changed code, and then switch to the bug's branch to solve the problem, and finally go back to this point using Git stash pop to reproduce the local change code that was not committed before;
However, the following two points need to be noted for using this command:
- Git stash just saves the file changes that have been git add, as long as the files are saved, then the local new files must be saved after using the git add command, otherwise you will be prompted to save the new files to be staged;
- The staging branch needs to use Git stash pop recovery, but if multiple git stash, you need to remember exactly where the corresponding save location of a branch, otherwise easy to be mistaken; you can use Git stash list to view;
4. Git revert
Use and revoke the content of the changes submitted. This revert is different from some of the files in the IDE, such as Webstrom, which revert git, which ultimately uses git checkout.
The biggest scenario for this command is when the code is released online, and for some reason it requires code rollback, when the code is rolled back to merge master based on the branch published on the current branch, the local new development requirement code is lost because the code is rolled back to the previous line, and there is no rollback code at this time. , how to restore the merge dropped code, then need git revert, the specific steps are as follows:
- View rollback-generated commits via Git reflog, such as a log that has been rolled back locally, e103c47 is a rollback of the newly generated commit point
- Use the command git revert e103c47 to recover the merge-out code
5. Git reset
The command is used to reset the contents of the specified range, can be manipulated or manipulated, and the three parameters of the command--hard 、--mixed 、--Soft represents the range of resets, which is also one of the most commonly used commands;
Use this command to remember that it modifies the commit history of git and may delete some of the historical commit points causing the unrecoverable .
- git reset [--mixed] Commitid Reset the content of staging area with Commitid content, the workspace content will not be overwritten, so as to achieve the change of revocation staging area ;
Scenario: The code of a change does not pay attention to add to staging area with git add, but wants to undo staging area content without causing the local workspace code to be lost, you can use git reset HEAD
- git reset--hard commitid Reset the contents of staging area and workspaces with Commitid content
Scenario: In a certain stability uncertain but full-featured framework, the use of package management mechanism to download the latest framework source code, developed a period of time to find a big problem, but then the code has git add to staging area, want to undo the workspace and staging area content changes, you can use git reset- -hard HEAD
- git reset Commitid--Path Replace the staging area corresponding file with the file contents of the specified Commitid, the inconvenience of the work area ;
Note: This action does not modify the commit history of Git
6. Git branch
Used to create, display branches, and other branch-related commands, the situation commonly used in work:
- Git branch branchname origin/master create a branch based on master
This is a situation where the branches are created in full development, and the process is cumbersome and requires:
git checkout branchname //switch to the new branch git push //Push the current branch to the server Git branch-u Origin HEAD //Build the current branch upload stream with the same branch as the server
In practice, however, individuals tend to have the following one, in only two steps:
git checkout-b branchname Origin/mastergit push-u Origin HEAD
- Git branch-vv View the local branch of the corresponding situation of the upstream, be sure to ensure that the local branch and the server branch of the corresponding situation, because do not set up a corresponding case, the new branch default and master is the corresponding relationship
- Git branch-remote is looking at all the branches of the local repository
- git branch-d branchname Delete local branch
Note: You need to switch to another branch to do its work and cannot delete the current branch at the current branch, which is not allowed
7. Git push
Used to synchronize the code with the server warehouse, the use of the command does not repeat, a common use of this command is to delete the server side of a specified branch, the command is as follows:
Git push origin space: Remote_branch_name
8. Git rm
This command is used to delete the files in the branch, he can delete only staging area files, but also can delete staging area and workspace files, different parameters, the decision to delete the scope of the file is also different, as follows:
git rm--cached filename //Delete staging area file, workspace will not delete git rm filename //delete staging area and workspace at the same time
git rm filename Scenario: To meet a demand, add some code files, in the development may accidentally add a useless new file to the workspace and git add to staging area, it is necessary to delete the staging area and workspace of the file;
9. Git commit
Used to submit staging area code to the local repository; say git commit--amend option
Git commit--amend usage scenario: In a request development submission, for some reason the result of the submission is not expected, need to resubmit, but do not want the previous commit to make the git commit history tree longer, then you can use git commit-- Amend to use the contents of staging area to correct the previous submission. If you do not use a commit with--amend, the result is that the git commit history tree will grow longer. The schematic diagram of git commit--amend is as follows:
10. Git blame
In a multi-person collaborative development project, you want to know the history of a file changes, you can use this command to complete, as follows:
The above is to see the file line1 line to Line2 line of code changes, if you see a local file of 192 to 193 rows of changes, see that ADAM23 has the latest changes
Described above are their own use of the more frequent git commands and corresponding scenes, of course, some circumstances may not be worth learning, or there are inappropriate places, these all hope that everyone criticize correct!!
Common commands for Git common scenarios