Master git thoroughly (2)


1、 Transmission

Mastering git thoroughly (1)
Master git thoroughly (3)

2、 Basic concepts

① Work area
The so-called work area isThe directory you can see on your computerCan be simply understood as our project root directory is the workspace.
② Version Library
The so-called version library is executed in the root directory of the projectgit initAfter the command, a. git directoryUnder normal conditions,This. Git directory is hiddenThisThe. Git directory is the repository
Temporary storage area
The so-called temporary storage area is stored in the. Git version Library DirectoryIndex fileSo we also call the staging areaIndex area

So, there are three steps to completing a submission, firstMake changes to the content in the workspace, and thenThrough the add command, the modification of the workspace is brought into the temporary storageAnd finallyThe modification of the buffer is submitted to the version library by commit command
Master git thoroughly (2)

3、 Status command

git statusThe status command is used to view the changes that have occurred in the current workspace and which of them have been added to the staging area. In short, the status command canSee which files in the workspace have not been trackedWhich tracked files have been modified and added to the staging area (currently in the workspace or in the staging area)

> git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
new file:  foo.txt

Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
modified:  index.html 

Untracked files:

 (use "git add <file>..." to include in what will be committed)


As you can see, bar.txt It’s a newly added file that hasn’t been tracked by GIT; index.html Files that have been tracked and have not been submitted to the staging area after being modified in the workspace; foo.txt After modification in the workspace, the modification is added to the staging area at the same time.

4、 Diff command

git diff: with no parametersgit diffOrders, yesCompare the differences between the workspace and the staging area. For example, a file in the workspace has been modified, butNot yet added to the staging areaIn this case, the workspace and the staging area are differentgit diffCommand to view the changes in the workspace.
git diff --cached: with the — cached parameter, the same asgit diff --stagedThe comparison isDifference between staging and last commit. For example, after the content of the workspace is modified,At the same time, the modification of the workspace is added to the staging areaAt this time, the staging area is different from the latest submission, because the latest submission is the last submission.
git diff head: The last commit represented by head (the latest commit)YesCompare (workspace and staging) with the last commitThat is, you can view the differences between the workspace, the staging area and the last submission at the same time.

5、 Back modification

① Cancels the modification of the workspace: if the contents of the workspace are modified, butThe change has not been added to the staging area, then you can go throughgit checkout -- <file>To cancel the modification of the workspace.
② Cancel the modification of the staging area: if the contents of the workspace are modified, andThe change has been added to the staging areaThat’s OKReset the buffer by the reset commandBecause the modification of the workspace was submitted to the staging area, there is a difference between the staging area and the last submission, so we can reset the staging area to the head location, that is, the location of the last submission, and then we can restore the staging area, that is, thegit reset head <file>If the file name is not added after the head, all the files in the cache will be reset.
③ Go back to a version: if you want to go back to a version after a series of operations, you cangit reset --hard <commit-id>Return to the specified commit location. Without — hard, you can return to the specified submission location, but you will not empty the workspace modification, only the temporary storage modification. With — hard, you can return to the specified submission location completely.

It should be noted that,Git reset can also pass in the — soft parameter, i.eSoft fallbackAfter backing down to the target version, the changes between them will not be discarded, or will be put into the temporary storage.

//Add 1 to a file to generate a submission
> git commit -m "A"
//In a file, after removing the newly added 1, generate B submission
> git commit -m "B"
//Soft fallback to previous commit
> git reset --soft head~1
//According to git status, the modification to remove 1 is still in the buffer
> git status
//Soft back to the previous commit again
> git reset --soft head~1
//From git status, you can see that there is no change, because adding 1 and removing 1 offset each other in the buffer
> git status

6、 . gitignore file

For some files in the project, we may not want to be tracked by the version library. We canCreate a new. Gitignore file in the root directory of the project, and thenAdd files that do not need to be tracked by the repository to the. Gitignore file. It should be noted that,The. Gitignore file can only ignore files that have not been tracked by the repository. What should I do if I want to ignore the files that have been tracked by the version library? Files that have been tracked by the repository cannot be ignored becauseThe file is in the staging area, so if we delete the file from the scratch area, it’s OK.

> git rm --cached <file>

> git add <file>

> git commit --message 'untrack some file'

However, there is a disadvantage of this methodAfter deleting from the staging area, you need to commit once. We can also get throughgit update-index --assume-unchanged <file>Then the file will not be tracked by the version library,You don’t need to add it to the. Gitignore file. If you want to recover, you can do itgit update-index --no-assume-unchanged <file>

7、 Stash command

When we are in the middle of something, we suddenly find thatYou need to fix a problem quickly firstBut we don’t want to abandon the previous work, so we can go through thegit stashcommandSave these workspace and staging changes locally. implementgit stashAfter the command, the workspace and temporary storage area will be cleaned immediately. At this time, you can start some more urgent bugs. After the bug is fixed, the previous work will be restored. You cangit stash popRestore the stash at the top of the stack. You can also pass thegit stash listView all stash, and then restore the specified stash, such as:

> git stash
Saved working directory and index state WIP on master: 3f5aa1b add bar.txt

> git stash list
[email protected]{0}: WIP on master: 3f5aa1b add bar.txt

> git stash pop [email protected]{0}

git stash popWhen the command is restored,The recovered stash will be removed from the list. andgit stash applyIt can also be used to restore stash, butIt does not remove stash from the list
When restoring, it can be restored not only on the original branch, but on any branchgit stash popIn the process of command recovery, if there is a conflict, stash will not be deleted from the list. But we can delete it manually and execute itGit stash drop < stash name >Just give the order.

//Remove stash @ {0} from stash list
> git stash drop [email protected]{0}

It should be noted that when we use stashIt stores the changes of files that have been tracked by git version libraryAnd files that are not tracked by git repository, such asNew fileYou don’t need stashEven if you use git stash command, you can’t stash the new fileIn other words, when we operate git,You don’t need to manage the files that are not tracked by git version library, you can operate them directly

When using git stash pop to restore the modification of the workspace:
IfThere are uncommitted changes in the current workspacealsoThis content conflicts with the content in the stash to be restoredSoStash will fail
If the uncommitted changes in the workspace are the same as those in the stash to be restoredThere is no conflictIf you want to succeed, you can stash it.
If there are no uncommitted changes in the workspace, butIf the contents in the stash to be restored conflict after restorationThen stash can succeed, butIt will not be removed from the stash list

8、 Reverse command

When we find a bug in a previous submission, we can use the revert commandCancel that submissionAndAutomatically append a commit record at the end of the branch. Unlike reset, reset is to reset the branch pointer. If the branch pointer is reset, all subsequent commits will be discarded and theReverse will only revoke that submission, and all subsequent submissions will be retained. If a file is modified in the submission of revert, and the file is also modified in the subsequent submission, there will be a conflict in the process of revert. Git will not only cancel the modification of the submission, but also cancel the modificationGo back to the previous submissionTherefore, the modification of the file submitted later may be discarded, such as:

//If the master branch submits the same file for three times, the first submission adds a new 1 in the first line, the second submission adds a new 2, and the third submission adds a new 3. Now it is necessary to reverse the second submission
> git revert head~1
//  At this time, there will be a conflict, because the third submission has also modified the file, and canceling the second submission only goes back to the content before the second submission, that is, there is only one 1 in the file. At this time, you should solve the conflict according to your needs. If you only want to remove 2, you can first keep the current modification (including 1, 2, 3), then solve the conflict, remove 2, and add it to the temporary file Storage area
> git add .
//Continue to reverse
> git revert --continue
//A commit record is generated at the branch head

After reverseThere is no conflictThe current modification is immediately undone and a commit record is generated.
The submission has been reversed, butThat change still existsIt only gives a chance to cancel the modification of that submission, and adds a submission at the endIf you reset the branch pointer to that submission through reset, you can still recover to the corresponding content of that submission.

At present, we are all reverting to ordinary submission, and there is another kind of submission generated during merging. For the submission generated automatically during merging, we can’t revert like ordinary submission, so we need to bring it with us-M parameterThe back root1 or 21 means to undo those changes on the inactive branch2 means to undo those changes on the currently active branchThat is, the modification on the current branch.

//If commit-1 is committed once on the master branch, and then a bug branch is created on this basis, commit-2 is committed once on the bug branch, and then switch back to the master branch for commit-3. At this time, the modification of the bug branch is merged on the master branch, and a secondary merge commit 181bb8 is automatically created. Now you need to reverse the merge commit
> git revert -m 1 181bb8
//At this point, the change commit-2 on the bug branch will be removed from the master branch again

It should be noted that although reverse cancels the merge submission, the bug branch is still in the merged state and can be executed at this timegit merge bugYou can’t merge the changes of the bug branch back again becauseIt has been merged. Revert just reverses the modified contentIf you want to merge back again, you canYou can also reverse the submission that was automatically generated during the merge submissionSo you can merge the changes on the bug branch back again.