Last article Talked about it. merge and rebase, We can already be in commit object The picture of composition ( Of course, I prefer to think of it as a tree ) It's a combination of branches , On the graph, we can add new nodes (git commit), Merge nodes (merge perhaps rebase), In this article, we will talk about moving and deleting nodes .
Before moving and deleting , Let's meet first HEAD The separation of .
The separation of HEAD
We all know ,HEAD It's pointing to the current branch , And separate HEAD Let it point to a specific commit record instead of a branch name .
Now my local submission record is like this
When we execute
git checkout 062704b1c3a814dfd95695aba3684c22e3f3fa85 after HEAD It's in a state of separation .
We can submit records by specifying hash To move the position of the pointer ( Whether it's a branch or HEAD), However, in reality, there is not such an intuitive diagram to show us , You have to use it
git log To view the submission records of hash, However hash And it's longer , fortunately git Yes hash It's more intelligent , We just need to provide the first few characters that uniquely identify the submission record , So we can just type in
git checkout 0627 You can check out the submission records .
adopt hash It is obviously not convenient to move nodes by using the value , therefore git Relative provides a reference to , So we can go from a place that's easy to remember ( such as bugFix Branch or HEAD) Start calculating .
It's awesome to quote , Two common usages
^Move up 1 Submission records
~numMove multiple submission records up
When we execute
git checkout master^ When , our HEAD It points to the last submission ( The current record is a record ), Notice that what's moving here is the submission record (commit object), In the same way, moving multiple records is the same .
The most common use of relative references is moving branches , We can direct the branch to another commit .
Now? master The branch points to the first commit , It should be noted that the current branch cannot be moved in the current branch , Otherwise you will make such a mistake
fatal: Cannot force update the current branch.
The branch will not be switched after the move , Still in the previous branch .
How can the submission tree be commit object Arbitrary movement ? Let's make our changes more casual ,
git cherry-pick It can be done .
Now we want to put bugFix On the branch C2,C4 's submission records moved to master On the branch , Just execute
git cherry-pick C2 C4
This command can " Copy " Commit the node and make an identical new commit in the current branch
Sometimes our code is submitted incorrectly , But it has been submitted to git Up there , What should I do if I want to go back ？ not so bad git There are two ways to undo changes ----
git reset as well as
git reset Undo by backing the branch record back several commit records changes , Actually, it's the pointer moving on the graph .
We were supposed to withdraw C2 The submitted , But why is there another one C2' What about submission ？ This is because of the new submission C2' Introduced changes -- This change is used to undo C2 This submitted , in other words C2' In the state of C1 It's the same .
revert Then you can change push I went to the remote warehouse and shared it with others .