A git
Sometimes, when we have been working on a branch for a while, it is suddenly discovered that the previous version has problems and needs to be fixed, and you need to switch to another branch. The problem is THAT I don’t want to commit just because of this one bug and I’m doing half the work, and git Stash solves that problem and allows us to switch branches without committing. As shown below:
At this point, I want to switch to the master branch, but because there are unfinished tasks on the Wang branch, I am not allowed to switch branches, as shown below:
But I don’t want to commit because the whole thing isn’t done yet. Here’s the git Stash implementation, as shown below:
Now pretend I have fixed the bug on the master branch and need to go back to the original working branch by executing the following command:
Git checkout wang // go back to the previous branch git Stash apply // apply the previous storageCopy the code
After applying the previous store, we need to remember to delete the store record by doing the following naming:
Git Stash drop Storage IDCopy the code
And then we can get back to work. Perfect!
Git regret medicine
Git workspace regret medicine
When we modify the files that are already managed by Git, it looks like this:
If you want to undo your changes by command at this point, execute the following command (as prompted in the figure above) :
Git restore <file name >Copy the code
Git cache
After we execute git add, the following image shows:
If this is to undo the contents of the cache, execute the following command (shown above) :
git restore --staged ameng.txt
Copy the code
Git repository regret medicine
Our commit record cannot be deleted (but can be left in a free state), as shown below:
Obviously, the above comment does not meet the requirements, execute the following command:
git commit --amend
Copy the code
The following screen appears, where you can edit comments.
Git reset –soft HEAD~, then recommit again, moving the HEAD and moving the branch with it so that the wrong commit object is in the free branch, as shown below:
Git reset can be found on git’s website
Free branch
As we switch to one of the specified commits, the HEAD will be in the “detached” state, or the detached state.
Pros and cons of HEAD dissociation
Benefits: When the HEAD is in a free state, developers can easily switch between historical versions, such as going back to a commit by using the corresponding or name.
Cons: If you commit on this basis, you open a new “anonymous branch”; That is to say, our commit cannot be saved visually. Once we switch to another branch, our commit cannot be traced back to the original free state.
For example, now I have a branch called master, as shown below:
Then I switched to checkout 56c9266 to add wang. TXT file, as shown below:
Git branch can be used to check the branch status. When you modify the file content and commit, you can see the following image:
If we now switch the branch to the master branch, it looks like this:
From the figure above, a commit on a free branch is not traceable.
When we switch to the free state, we should create a new branch, and all our changes and commits will be saved to that branch. The HEAD will point to the commit ID of the branch, and will not be in the free state.
Please refer to this article for detailed explanation of free branches.