Git rebase The most detailed usage of GIT commands.

Source: Internet
Author: User
Tags using git git commands



Reprint please specify the source: Alvin Lee's Blog: Http://blog.csdn.net/lihaoweiV


The git rebase command is powerful, and in the Git Authority Guide, change the order of submission, which teaches a very complicated method, using git reset ... git cherry-pick ... Wait for the order.

But if you use git rebase command, then it's done.

Use the example below to explain one of the uses of Git rebase,

Change the order of commit (commit) ****************************************

git log View commit log:

Existing:

Commit a hello (it replaces the long hash code with the letter ABCDE)

Commit B Hi

Commit C how are

Commit D I am fine

Commit E Bye

Now you want to change D to B before, i.e. A, D, B, C, E,

We can use Git rebase-i [commit] because now we want to change the position of D, so we're going to rebase to commit e where the specific command is:

Git rebase-i E

The following text appears when you press ENTER

Pick A Hello
Pick B Hi
Pick C how to Are you
Pick D I am fine

Pick E Bye

# Rebase 23350be. 92c4c19 onto 23350BE (the following sections may vary depending on the situation)
#
# Commands:
# p, pick = Use commit
# r, reword = use commit, but edit the ' commit '
# 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
#
# If You remove a line here, that COMMIT would be LOST.
# However, if you remove everything, the rebase would be aborted.
#



To get D to the front of B, just change the position of the pick statement above and replace it with the following

Pick A Hello

Pick D I am fine
Pick B Hi
Pick C how to Are you
Pick E Bye


Then save the exit, if you determine the location of the replacement will not conflict, the following words will appear to be successful

Successfully rebased and updated refs/heads/[your branch name].


You git a log again and look at the order now

Commit A Hello

Commit D I am fine

Commit B Hi

Commit C how are

Commit E Bye


Of course, you can use this method to replace any order you want, but you have to make sure that there is no conflict after transposition. If there is a conflict, it will not appear successfully words.



Here's another way to use Git rebase.

Modify submission (non-sticky submission) content (including title, author, code, etc.) and update submit ************

Existing

Commit A Hello

Commit B Hi

Commit C how are u

Commit D Bye


I want to change the code and title of commit C, but I don't want to bother with git reset command,

You can also use the Git rebase-i [commit] command here

The specific:

git rebase-i d because I want to modify C, so I'm going to rebase to C's previous commit, i.e. D.

When you press ENTER, you will still see the same text as above


Pick A Hello
Pick B Hi
Pick C how to Are you
Pick D Bye


# Rebase 23350be. 92c4c19 onto 23350BE (the following sections may vary depending on the situation)
#
# Commands:
# p, pick = Use commit
# r, reword = use commit, but edit the ' commit '
# 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
#
# If You remove a line here, that COMMIT would be LOST.
# However, if you remove everything, the rebase would be aborted.
#
Here, change the pick in front of C to edit, which is

Pick A Hello
Pick B Hi
Edit C how are
Pick D Bye

Then, when you save the exit, the following words appear:

You can amend the "Commit Now" with


git commit--amend


Once you are satisfied with your changes, run


Git rebase--continue


Now what you want to modify.


If you want to change the title of a commit, note, author signature, etc., please use

git commit--amend command

For example, I'm going to change the title of commit C to Hello, I m Alvin Lee. After you have modified the above order,

Then use git rebase--continue to make your modifications effective.


If you find that the commit C code has a bug and want to modify the bug, such as a bug in Driver/alvin.c, open the file directly:

int main (void)

{

PRINNTK ("Hello I am Alvin lee!\n")

return 1;

}


Modify the error:

PRINTK ("Hello I am Alvin lee!\n");

Save exit. Add your modifications to registers (index) with the git add command.

Then use the git rebase--continue command to make your modifications effective,

If there is no conflict, then all OK.

Now look at the git log-p [commit] command,

int main (void)

{

PRINTK ("Hello I am Alvin lee!\n");

return 1;

The error has been modified over.



More usage, and listen to let's ***************************************************






1. Background of the occurrence:

When you submit the code, the administrator discovers that your code cannot be submitted to the server, mainly because some of the commits in your commit and server are no longer on the same timeline, that is, some of your commits are to be inserted between certain commits in the server, This can result in code conflicts. So this time you're going to use git rebase.

If, the branch you normally use is called new, then you have just submitted a few commits on this branch.

Practice:

1. Create a new branch, and code is synchronized with the server

git Checkout origin/v2.0-b Temp

2. To ensure that the new temp branch code is up to date, you can perform one of the following steps

Git pull

3. When you create a new branch, the system automatically checkout to the Temp branch, at which point

git checkout New

4. Merge code, and organize

Git rebase temp//will merge the Temp branch code and sort it in the order submitted

5. Because the order is rearrangement, there is a certain conflict.

6. Resolve conflicts, finally git add *, but don't want git commit

7. After resolving, perform git rebase--continue

8. Resubmit code: git push for-*


Note: If you want to rearrange the commit of some code

1. You can remember a commit number

2. Git rebase-i Commit

3. Will display a collation submission of the interface, there are many parameters, E. P. Wait a minute

4. Change the previous argument to E. The system will automatically allow you to wq the commit content after it has been saved

5. After the modification is complete, then git push for-*

==============================

Prev Next Git Community book Chinese version
rebase

Suppose you now create a branch called "MyWork" based on the remote branch "origin".

$ git Checkout-b mywork origin

Now we make some modifications in this branch, and then generate two commits (commit).

$ VI file.txt
$ git commit
$ VI otherfile.txt
$ git commit
...

But at the same time, some people have made some changes in the "Origin" branch and submitted them. This means that the two branches of "origin" and "mywork" are "forward" and "forked" between them.

Here, you can use the "pull" command to pull down the changes on the "origin" branch and merge with your modifications; The result looks like a new "merged commit" (merge Commit):

However, if you want the "MyWork" branch history to look like it didn't go through any merging, you might be able to use Git rebase:

$ git checkout mywork
$ git rebase origin

These commands will cancel each commit (commit) in your "mywork" branch and temporarily save them as Patches (patch) (which are placed in the ". Git/rebase" directory), and then update the "mywork" branch to the latest "origin" branch. Finally, the saved patches are applied to the "MyWork" branch.

When the ' mywork ' branch is updated, it points to these newly created commits (commits), and those old commits are discarded. If you run the Garbage collection command (pruning garbage collection), these discarded commits are deleted. (See git GC)

Now we can take a look at the historical differences that are produced by merging (merge) and using rebase:

In the process of rebase, there may be a conflict (conflict). In this case, Git stops rebase and lets you resolve the conflict, after resolving the conflict, with the "git-add" command to update the content index (index), then you do not need to perform git-commit, as long as the execution:

$ git rebase--continue

This way, GIT will continue to apply the remaining patches.

At any time, you can use the--abort parameter to terminate the Rebase action, and the "MyWork" branch will return to the state before rebase started.

$ git rebase--abort

Gitcast:c7-rebase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.