Home  >  Article  >  Development Tools  >  What is the usage of rebase in git

What is the usage of rebase in git

WBOY
WBOYOriginal
2022-01-04 16:58:0618728browse

In git, rebase can edit, delete, copy, and paste a certain linear commit history. It is often used to merge commits and paste a certain commit to another branch. The syntax is "git rebase parameter [startpoint][endpoint]".

What is the usage of rebase in git

The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.

What is the usage of rebase in git

Rebase is a very charming command in git. Use it properly and you will greatly improve yourself. work efficiency; on the contrary, if used indiscriminately, it will cause trouble to other people in the team. Its function is briefly summarized as follows: you can edit, delete, copy, and paste a certain linear submission history; therefore, the reasonable use of the rebase command can make our submission history clean and concise!

Premise: Do not modify any commits that have been submitted to the public warehouse through rebase (except for branches you play alone)

1. Merge multiple commits into one complete commit

When we have submitted multiple times in the local warehouse, before we push the local submission to the public warehouse, in order to make the submission record more concise and clear, we hope to put the following three submission records of branches B, C, and D. Merge into a complete commit and then push to the public repository.

What is the usage of rebase in git

Now that we have added four commits on the test branch, our goal is to merge the last three commits into one commit:

What is the usage of rebase in git

Here we use the command:

  git rebase -i  [startpoint]  [endpoint]

where -i means --interactive, which pops up an interactive interface for the user to edit and complete the merge operation, [startpoint] [endpoint] is specified An editing interval, if [endpoint] is not specified, the end point of the interval is by default the commit pointed to by the current branch HEAD (note: this interval specifies an interval that is open at the beginning and closed at the end).

After viewing the log, we run the following command:

git rebase -i 36224db

or:

git rebase -i HEAD~3

Then we will see the following interface:

What is the usage of rebase in git

The uncommented part above lists all the submissions included in our rebase operation. The comment part below is the command description provided by git for us. The pick in front of each commit id indicates the instruction type. Git provides us with the following commands:

  • pick: retain the commit (abbreviation: p)

  • reword: Keep the commit, but I need to modify the comment of the commit (abbreviation: r)

  • ##edit: Keep the commit, but I want to stop and modify the commit (Not just modifying comments) (abbreviation: e)

  • squash: merge this commit with the previous commit (abbreviation: s)

  • fixup: merge this commit with the previous commit, but I don’t want to keep the comment information of the commit (abbreviation: f)

  • exec: execute shell command (abbreviation: x)

  • drop: I want to discard the commit (abbreviation: d)

According to our needs, we edit the commit content as follows:

What is the usage of rebase in git

Then there is the comment modification interface:

What is the usage of rebase in git

After editing and saving, the commit merge is completed:

What is the usage of rebase in git

Recommended study: "Git Tutorial"

The above is the detailed content of What is the usage of rebase in git. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn