Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What practical skills does Git have?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the practical skills of Git". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what practical skills Git has.

Compress several commit into one

⚠️ one thing to pay special attention to here is that rebase leads to the generation of new commit nodes, so remember not to rebase remote branches that are shared by many people.

Rebase-I 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 some commit, sort commit, and so on. The specific commands are as follows, and the mode of operation is the same as that of the motion diagram, which is edited in vim. It is not unfolded here, and interested students can operate it by themselves.

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

In addition, if you want to merge the most recent commit, we can also use git reset-- soft HEAD~3 & & git commit-m 'xxx'. Students with this problem can think about it with reference to the visualization method emphasized by Git's internal principles.

Retrieve lost commit nodes or branches

For example, after rebase in the previous step, it is found that it does not meet expectations, how to recover? Accidentally delete a branch, how to find it?

"if you learn this skill, your colleagues will buy you milk tea, and you may even get a girl." -- A student from a previous course

The main idea is to find the hash value of the commit object you want to return, and then perform a git reset restore.

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

Get a clean workspace

When we experiment with an idea, or when we talk to friends about the code, we may modify the code at will. When we get back to normal development, we need a clean working directory, that is, to make sure that the current working directory is the same as the last commit file in Git. What can we do?

Use as few operations as possible that will lose files unless you are sure they are no longer needed.

Modify the most recent commit

Commit finally found that there are some temporary log forgot to remove? Did you forget to add some files? Wrong words appear in commit information?

You can also use git reset HEAD~, and then perform the changes you need, and then commit, which is the same as the command described above.

Submit some changes in a file

Git interactive add also has a lot of features, and it is recommended that you have time to try it.

It is prohibited to modify remote branches that are shared by many people.

If a remote branch is shared by more than one person, do not execute commands such as reset, rebase, and so on that will modify the existing commit object of the branch.

Refer to this article Rebase and the golden rule explained for a specific explanation.

Undo a merger

If it is a local branch, just git reset-- hard.

If the branch has been pushed to the remote end, such as merging into master, it is found online that there is a bug that needs to be rolled back. At this point, the branch may have been used by others, according to "prohibit modifying multi-user shared remote branches", you need to execute git revert-m 1 and add a new revert node, such as E' in the following figure.

However, be careful not to continue development in the original feature branch, but should delete the original branch, pull a new branch from the E' node to do bug repair, and so on.

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

Delete a file from the entire history

The code is going to be open source, but what if you find it includes a key file or an intranet ip?

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

You can use the filter-branch command, which works by taking each commit checkout out, then executing the command you gave it, like rm-f passwords.txt above, and then commit it back.

⚠️ is a high-risk operation that modifies the chain of historical change records to produce a new commit object. So please notify all developers in the warehouse before execution, and after execution, all developers will continue to develop from the new branch and discard all previous branches.

Other useful commands

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

Git bisect binary to find the problem change node, for example, if you find that the current test in advance is not passed, but the HEAD~10 (10 pre-commit) tests can pass, you can use git bisect to help you locate the change point where the problem occurs.

Git blame looks at who last modified a line of code.

Git show-branch visually shows the relationship between multiple branches.

Git subtree splits or merges warehouses.

At this point, I believe you have a deeper understanding of "what practical skills Git has". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report