撤销git add/commit 与更新从别人仓库克隆的项目

1.撤销 git add

撤销操作

git status #先看一下add 中被add了哪些文件 
git reset HEAD #如果后面什么都不跟的话 就是上一次add 里面的全部撤销了 
git reset HEAD XXX/abc.python #就是对某个文件进行撤销了

演示:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   teset.py


$ git reset HEAD teset.py
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        teset.py # 提示你可以用 git add添加,说明撤销成功

2.撤销 git commit

git reset --soft HEAD^ # 这样就成功的撤销了你的commit(未撤销add)

注意:
a. 仅仅是撤回commit操作,您写的代码仍然保留(工作空间的代码还在)。
b. HEAD^的意思是上一个版本,也可以写成HEAD1,如果你进行了2次commit,想都撤回,可以使用HEAD2
c. --mixed:不删除工作空间改动代码,撤销commit,并且撤销git add 操作
d. --soft: 不删除工作空间改动代码,撤销commit,不撤销git add .
e. --hard: 删除工作空间改动代码,撤销commit,撤销git add .完成这个操作后,就恢复到了上一次的commit状态。

演示:

# ------------提交 commit----------------
$ git add teset.py
$ git commit -m 'test'

[master 9ff1ee5] test
 1 file changed, 8 insertions(+)
 create mode 100644 teset.py

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 comm

# ------------撤销 commit----------------
$ git reset --soft HEAD^
$ git status

On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   teset.py

顺便说一下,如果commit注释写错了,只是想改一下注释,只需要:git commit --amend此时会进入默认vim编辑器,修改注释完毕后保存就好了。

3.更新从别人仓库克隆的项目

背景:在github上克隆了别人的代码,当别人更新后,我们需要再将其从github上更新到自己的本地仓库。

第一步:首先使用git remote add upstream 原作者仓库把别人的仓库添加到你的上游远程,通常命名为upstream。(这部操作只需一次即可,以后再次更新时不用);
第二步:使用git remote -v就可以看到一个origin是你的,另外一个upstream是原作者的;
第三步:使用git fetch upstream去拉原作者的仓库更新;
第四步:使用git checkout master切换到自己的master
第五步:使用git merge upstream/master合并到你自己本地的master,这样自己本地的代码就得到更新了

猜你喜欢

转载自blog.csdn.net/The_lastest/article/details/84398911