How to Use git to roll back some modifications

Source: Internet
Author: User
Tags how to use git
How to Use git to roll back some modifications ()

 

Many times, git novice easy to misoperation, for example, in the levelIISZ-1.4.dev branch, run the GIT pull IDC cpp-1.0 results, so do a lot of trouble, often lead to incorrect Maven project format, at this time, you can use git reset -- hard to cancel this modification.
However, this problem also exists. Some local modifications that have not been submitted may disappear. You can try the GIT revert command

Reset refers to Resetting the content of the current head without any trace.

Sets the current head to the specified commit and optionally resets the index and working tree to match.

Git reset -- hard head ~ 3

It resets all the latest three submissions, just as it has never been submitted.

Working tree, index, and head are reset according to -- soft -- mixed -- hard.

Revert revokes a certain submission, but this withdrawal will also be saved as a Commit (so that the original modification will not be lost, but no submitted content ?).

//////////////////////////////////////// //////////////////////////////////////// //

A very good article

Http://alpha-blog.wanglianghome.org/2010/07/30/git-partial-rollback/

How to Use git to roll back some modifications

When people make mistakes, if they find that they accidentally submit private goods to the public code library, how can they be recovered?

For example, the following code library:

$ Mkdir Git-partial-revert
$ CD Git-partial-revert
$ Echo "hello a"> a.txt
$ Echo "Hello B"> B .txt
$ Git init
Initialized empty git repository in/home/liang/project/git/Git-partial-revert/. Git/
$ Git add *. txt
$ Git commit-M "initial version"
[Master (root-commit) b5e1a24] initial version
2 files changed, 2 insertions (+), 0 deletions (-)
Create mode 100644 a.txt
Create mode 100644 B .txt
$ Git log
Commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:20:38 2010 + 0800

Initial version

The following is an incorrect modification.

$ Echo "Hello A from B"> a.txt
$ Echo "Hello B from a"> B .txt
$ Git commit-a-m "Hello A from B"
[Master df3144e] Hello A from B
2 files changed, 2 insertions (+), 0 deletions (-)
$ Git push origin master
$ Git log
Commit df3144e4158f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:22:51 2010 + 0800

Hello A from B

Commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:20:38 2010 + 0800

Initial version

If the error occurs, only the.txt modification is required. The B .txt modification is a private goods and should be made locally and submitted later.

The first method is manual modification. Depending on the amount of content to be modified, this may be the simplest or the most stupid method.

If the error occurs in the latest commit, you can use git reset to modify it. As follows:

$ Git reset b5e1a24f -- B .txt
Unstaged changes after reset:
M B .txt
$ Cat B .txt
Hello B
Hello B from
$ Git log
Commit df3144e4158f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:22:51 2010 + 0800

Hello A from B

Commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:20:38 2010 + 0800

Initial version
$ Git diff
Diff -- git a/B .txt B/B .txt
Index b1bdbca .. ab47375 100644
--- A/B .txt
++ B/B .txt
@-1 + 1, 2 @@
Hello B
+ Hello B from
$ Git status
# On branch master
# Changes to be committed:
# (Use "Git reset head <File>..." To unstage)
#
# Modified: B .txt
#
# Changed but not updated:
# (Use "Git add <File>..." to update what will be committed)
# (Use "Git checkout -- <File>..." to discard changes in working directory)
#
# Modified: B .txt
#
$ Git diff -- cached
Diff -- git a/B .txt B/B .txt
Index ab47375.. b1bdbca 100644
--- A/B .txt
++ B/B .txt
@-1, 2 + 1 @@
Hello B
-Hello B from
$ Git commit-M "revert change in B"
[Master d49f9f2] revert change in B
1 files changed, 0 insertions (+), 1 deletions (-)
$ Git push origin master
$ Git status
# On branch master
# Changed but not updated:
# (Use "Git add <File>..." to update what will be committed)
# (Use "Git checkout -- <File>..." to discard changes in working directory)
#
# Modified: B .txt
#
No changes added to commit (use "Git Add" and/or "Git commit-")
$ Git diff
Diff -- git a/B .txt B/B .txt
Index b1bdbca .. ab47375 100644
--- A/B .txt
++ B/B .txt
@-1 + 1, 2 @@
Hello B
+ Hello B from
$ Git log -- stat
Commit d49f9f2531ed9106ea53006bd698bbcdd54698e9
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:34:43 2010 + 0800

Revert change in B

B .txt | 1-
1 files changed, 0 insertions (+), 1 deletions (-)

Commit df3144e4158f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:22:51 2010 + 0800

Hello A from B

A.txt | 1 +
B .txt | 1 +
2 files changed, 2 insertions (+), 0 deletions (-)

Commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:20:38 2010 + 0800

Initial version

A.txt | 1 +
B .txt | 1 +
2 files changed, 2 insertions (+), 0 deletions (-)

At this time, B's modifications are removed from the public code library, but they are retained locally.

You can also use git revert. The following operation is not reserved for B .txt modifications. How to retain the changes for readers as exercises :-)

$ Git revert-N df3144e31
Finished one revert.
$ Git status
# On branch master
# Changes to be committed:
# (Use "Git reset head <File>..." To unstage)
#
# Modified: a.txt
# Modified: B .txt
#
$ Git diff -- cached
Diff -- git a/a.txt B/a.txt
Index 2c5e468 .. 74614c9 100644
--- A/a.txt
++ B/a.txt
@-1, 2 + 1 @@
Hello
-Hello A from B
Diff -- git a/B .txt B/B .txt
Index ab47375.. b1bdbca 100644
--- A/B .txt
++ B/B .txt
@-1, 2 + 1 @@
Hello B
-Hello B from
$ Git reset head a.txt
Unstaged changes after reset:
M a.txt
$ Cat a.txt
Hello
$ Git checkout -- a.txt
$ Cat a.txt
Hello
Hello A from B
$ Cat B .txt
Hello B
$ Git commit-M "revert change in B"
[Master 5f6a2e1] revert change in B
1 files changed, 0 insertions (+), 1 deletions (-)
$ Git log -- stat
Commit 5f6a2e16bfd59281d0150e3644aa1485dd6c0078
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 14:17:44 2010 + 0800

Revert change in B

B .txt | 1-
1 files changed, 0 insertions (+), 1 deletions (-)

Commit df3144e4158f6ec189ed0b2b57908d8d4e862fe5
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:22:51 2010 + 0800

Hello A from B

A.txt | 1 +
B .txt | 1 +
2 files changed, 2 insertions (+), 0 deletions (-)

Commit b5e1a24fc65202977b903435750dd9fb5e0d04d0
Author: Liang Wang <[email protected]>
Date: Fri Jul 30 13:20:38 2010 + 0800

Initial version

A.txt | 1 +
B .txt | 1 +
2 files changed, 2 insertions (+), 0 deletions (-)

If the incorrect modification is not the latest commit, you can only use git revert.

//////////////////////////////////////// /////////////////////////////////////

Our teamleader summary is very good !!!

How to Use git to roll back some modifications ()

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.