This is the real Git-Git practical skills

Author: lzaneli, Tencent TEG front-end development engineer

This article is the last article in this series. The previous article mainly talked about the basic principles (see the album above). On the basis of understanding the principles, I will introduce some practical skills to everyone, hoping to improve your development efficiency.

Because this article is more about enumerating practical techniques, the structure of the article will appear to be a little messy, and it will not require everyone to read in order like the previous two articles. Each point is independent of each other, and everyone can learn according to their own needs.

In this article, I will use the method of operating screen recording to introduce examples. I hope this method can give you a more intuitive understanding of how to use commands.

Compress several commits into one

⚠️ There is one point to pay special attention to here: rebase will cause new commit nodes to be generated , so remember not to rebase remote branches shared by multiple people.

rebase -iIt is a very practical and widely used tool, I hope everyone will learn how to use it. It can also be used to modify commit information, discard certain commits, sort commits, and so on. The specific commands are as follows, the operation mode is the same as the animation, and they are all edited in vim. Not to expand here, interested students can do it by themselves.

# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.

Also, if you want to merge several of the most recent commit, we can use git reset --soft HEAD~3 && git commit -m 'xxx'to achieve. For this problematic student, you can refer to the visualization method emphasized by Git's internal principles .

Retrieve the missing commit node or branch

After rebase in the previous step, it is found that it does not meet expectations. How to recover? I accidentally deleted a branch, how do I get it back?

"Learn this skill, your colleague will invite you to drink milk tea, and maybe you can harvest girls."-A classmate from a previous course

The main idea is: find the hash value of the commit object to be returned, and then perform the git resetrecovery .

We know that the emergence of Git is to ensure that our operations are not lost as much as possible. In the internal principle of Git , we have said that once a git object is created, it cannot be changed, so as long as you find its corresponding hash value, you can retrieve it. . But what about ref? We also mentioned in Git internal principles that it is a variable pointer. For example, if you submit a commit in the master, the ref of the current master will point to the hash value of the new commit object. reflog is the variable pointers to these historical record, can be understood as log ref's , it can also be understood as version control version control .

Get a clean working space

When we experiment with an idea or talk to a friend about the code, we may modify the code at will. And when we return to normal development, we need a clean working directory, that is, to ensure that the current working directory is consistent with the last commit file of Git. What can we do?

Try to minimize operations that will lose files, unless you can be sure that these files are no longer needed.

Modify the last commit

After commit, I found that there are some temporary logs that I forgot to remove? Some files forgot to add? A typo in the commit message?

You can also use it git reset HEAD~, then execute the modifications you need, and then commit. The effect is the same as the command described above.

Submit partial changes in a file

Git interactive add has many more features, and I recommend you to try it out if you have time.

It is forbidden to modify the remote branch shared by multiple people

If a remote branch is shared by multiple people, do not execute reset, rebase, etc. commands that will modify the existing commit object of this branch.

For specific explanation, refer to this article Rebase and the golden rule explained .

Undo a merger

If it is a local branch, just need it git reset --hard <合并前的SHA1>.

If this branch has been pushed to the remote end, for example, merged into the master, it is only found that there is a bug that needs to be rolled back when it is posted online. At this time, the branch may have been used by other people. According to "Prohibit to modify the remote branch shared by multiple people", you need to execute git revert -m 1 <合并的SHA1>and add a revert node, like E'in the figure below.

But be careful not to continue development on the original feature branch. Instead, delete the original branch and pull out a new branch from the E'node for bug fixes.

If you continue to develop on the original feature branch, you need to do a revert operation to revert the E'node and turn it into E'' (as shown below) when merging back to the master, otherwise it is easy to lose files and other problems. The specific reason analysis refers to the summary in the branch merge .

Delete a file from the entire history

The code is going to be open source, but it includes a key file or intranet ip, what should I do?

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

You can use the filter-branch command, its implementation principle is to check out each commit, then execute the command you gave it, like the above rm -f passwords.txt, and then commit back again.

⚠️ This operation is a high-risk operation, which will modify the historical change record chain and generate a new commit object. So please notify all developers in the warehouse before execution. After execution, all developers continue to develop from the new branch and discard all previous branches.

Other useful commands

The following commands are also more practical commands, and interested students can learn by themselves.

  • git bisectBinary search Change node problem occurs, for example, you find that the current advance under test is not passed, but the test HEAD ~ 10 (10 front-submission) is through, we can use git bisectto help you navigate to change the point of problems .

  • git blame See who modified a line of code last.

  • git show-branch Visually show the relationship between multiple branches.

  • git subtree Split or merge warehouses.


I hope you will find something after reading. Interested students can read other articles in the same series:

reference

Guess you like

Origin blog.csdn.net/Tencent_TEG/article/details/108162181