编程知识 cdmana.com

Git (5) -- free modification and submission record

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

git log

When we execute git checkout 062704b1c3a814dfd95695aba3684c22e3f3fa85 after HEAD It's in a state of separation .

 Separate HEAD

 Separate HEAD

Mobile nodes

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 .

Relative reference

It's awesome to quote , Two common usages

  1. Use ^ Move up 1 Submission records
  2. Use ~num Move multiple submission records up

 Move a node

 Moving multiple nodes

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 .

 Move branch to specified node

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 .

Move freely

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

git cherry-pick

Fallback code

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 revert.

git reset

git reset Undo by backing the branch record back several commit records changes , Actually, it's the pointer moving on the graph .

git reset

git revert

git revert

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 .

版权声明
本文为[think123]所创,转载请带上原文链接,感谢

Scroll to Top