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

How git modifies History

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how to modify the history of git". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Methods: 1, use the "git commit-amend" command to modify the history record; 2, use the "git rebase-I to specify the submission number" command to modify multiple history records; 3, use the "git filter-branch-the scope modified by the filer command" command to rewrite the history record.

This article operating environment: Windows10 system, Git2.30.0 version, Dell G3 computer.

How git modifies History

Junior contestant git amend

If you only find that your hand is disabled after the submission, you can use git commit-amend to modify the previous submission. After the command is executed, it removes the previous commit from the current branch, restores the workspace to the last ready commit state (and mixes the changes since the last commit), and then displays an vim interface for you to modify the last commit information. Once saved in vim, all current changes will be submitted with the new submission information.

This command can only modify the last commit, and one command is equivalent to performing the following series of actions:

$ORIG_HEAD= `git show` # Save the current commit number of this submission $git reset-soft head ^ # return to the state before the last submission was ready for submission $. # do some operations and modify $git commit-c $ORIG_HEAD# to use the last submission information as the submission information, but will call up the editor interface

Intermediate contestant git rebase

The previous git commit-amend can only modify the last submission, but when I lost my hand all the way on a whim, this command will not save us. At this time, we have to sacrifice the git rebase-I to help us modify the bad past.

In fact, git rebase-I is not a command specifically used to modify history, but a command that allows us to perform derivative operations interactively (one by one), but we can use this command to modify the submission history.

This command is used as git rebase-I. For example, you can use git rebase-I HEAD~3 to modify this submission, the last submission, and the last submission for a total of three times.

This is the interface of a vim editor, and here's how to edit this script. The commands you can use are the six commands in the comments below. When you exit the page that is currently edited, git will perform the corresponding actions on the submission one by one based on the script (starting with the earliest commit).

If you just want to change the submission information, change all the pick to r, then: wq save, and then git will let you start with the earliest submission information one by one.

In the remaining commands, e will pop out of the vim editor when the corresponding submission is modified, and the HEAD pointer points to the submission. You can use git commit-amend to modify the submission, and then execute git rebase-continue to continue the next operation. S will merge the submission and its parent submission into one submission when modifying the corresponding submission. F is similar to s but ignores the information currently submitted and directly takes the information submitted by the parent; x requires you to enter a command after x and execute it when HEAD points to this submission. You can also use these commands to reschedule and split submissions.

The ultimate killer git filter-branch

If we suddenly find that our mailboxes have been miswritten (╯ °□ °) ╯ submission ┻━┻ after we have submitted them for many times, if we use the commands mentioned earlier, we will probably be exhausted before we finish correcting them. At this point we can use git filter-branch to rewrite the branch, which can perform our preset operations on each submission in batches.

The basic format used by the git filter-branch command is git filter-branch-- 'command', and different filter provides different inputs and outputs to the command. For example,-- msg-filter indicates the modification submission information, the original submission information is read from the standard input, and the new submission information is output to the standard output;-- tree-filter represents the list of modification files, and so on. Finally, there will be a scope for rewriting. For example, git filter-branch-env-filter 'GIT_AUTHOR_EMAIL=john@example.com export GIT_AUTHOR_EMAIL' HEAD can be used to rewrite mailboxes. It is recommended that you try a branch before invoking the command and then perform the operation on the branch you want to modify.

This is the end of "how git modifies History". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report