您的当前位置:首页正文

Git版本提交上去被打回怎么回滚

2024-11-09 来源:个人技术集锦

在一个项目比较大的时候或者项目在现场版/使用版本上做修改的时候,就需要非常慎重了,一方面可能新来的开发人员不了解代码结构去做修改,很可能增加新的bug甚至导致应用崩溃,所以很多公司会加入审核机制,如果审核不通过提交就会被打回,那么打回后Git怎么回退到原来的状态呢?

#查看当前提交版本

确保当前分支上最新的提交是你需要撤销的提交,如果不是或者别人也有提交,那么你在撤销的时候会带过来别人的代码,那么这个时候就不能保证你的顺利撤销了,可能还会产生冲突之类的,这个切记;

我们使用Git log查看最新的提交:

E:\Development\Code\SxxxxYx_2018>git log .
commit 550416381e6aa292544a13b89c05692cc660e089 (HEAD -> develop)
Author: zhonglunshun <zhonglunshun@hotmail.cn>
Date:   Wed Oct 17 16:21:58 2018 +0800

    <E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4><E4>
    <E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5>
    1.<E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5>(>2)
    2.<E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5>
    3.<E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5>?
    4.<E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5><E5>?

    Change-Id: I41cabbc05901d10b4f6053fa18a672cf5a291944

commit 2c4b2cf4581bfc98bce09dde98bf4510ae4ec178
Author: zhonglunshun <zhonglunshun@hotmail.cn>
Date:   Tue Oct 16 16:07:32 2018 +0800

我们可以清楚的看到,最新的提交是我需要撤销的代码,我们拿到最新一条记录的前一个commitID,2c4b2cf4581bfc98bce09dde98bf4510ae4ec178,撤销用得上,接下来我们使用git reset commitID就能把push撤销;

撤销Push

E:\Development\Code\hotmail_2018>git reset --soft commitID

E:\Development\Code\hotmail_2018>git reset --soft 2c4b2cf4581bfc98bce09dde98bf4510ae4ec178

E:\Development\Code\hotmail_2018>git status .
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/bmap/BaiduMapHelper.java
        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/bmap/MapPresenter.java
        new file:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayGpsCacheWriter.java
        new file:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayPointWrapper.java
        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayTestHelper.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        subway2.json

然后我们使用git status查看一下撤销push后的状态,检查一下是否成功,这里我们可以看到成功了的;

git status .

On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/bmap/BaiduMapHelper.java
        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/bmap/MapPresenter.java
        new file:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayGpsCacheWriter.java
        new file:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayPointWrapper.java
        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayTestHelper.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        subway2.json

撤销Commit

撤销push之后我们需要用Git status查看确认一下。然后使用git reset HEAD <file>撤销提交;也就是说我们可以使用这条指令撤销上一步操作,push上一步操作是commit和add,所以执行后可以撤销commit和add;

git reset HEAD

执行结果如下:

E:\Development\Code\hotmail_2018>git reset HEAD
Unstaged changes after reset:
M       app/src/main/java/com/hotmail/android/signaltrack/app/bmap/BaiduMapHelper.java
M       app/src/main/java/com/hotmail/android/signaltrack/app/bmap/MapPresenter.java
M       app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayTestHelper.java

这个时候,我们把commit撤销了,那么我们查看下状态,使用git status命令;

E:\Development\Code\hotmail_2018>git status .
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/bmap/BaiduMapHelper.java
        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/bmap/MapPresenter.java
        modified:   app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayTestHelper.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayGpsCacheWriter.java
        app/src/main/java/com/hotmail/android/signaltrack/app/subway/SubwayPointWrapper.java
        subway2.json

no changes added to commit (use "git add" and/or "git commit -a")

到这里我们就把代码回滚到未提交状态了;

Top