首先来了解两个概念:
$ git status On branch master 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: readme.txt Untracked files: (use "git add <file>..." to include in what will be committed) LICENSE no changes added to commit (use "git add" and/or "git commit -a") Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked。
2、
commit qaws1a4b1e251f07e4e8658fd2b1d6d0918ca44f (HEAD -> test_qy, origin/test_all, test_all) Author: qy <quyang@123.com.cn> Date: Thu Apr 25 13:49:10 2019 +0800 member page change commit qwsedeed7b208331ead8cc469fd568f0d4c2da05 Author: qy <quyang@123.com.cn> Date: Thu Apr 25 11:44:50 2019 +0800 member strategy page style change commit 417fce1c1b1171f5ed9f5a5a6cb31e44ec41da77 Author: zhangsan <zhangsan@123.com.cn> Date: Thu Apr 25 11:26:49 2019 +0800 modify some file commit 76ytd819f40a633d4ec3fc8f9d7662c6f234c0fb Merge: eb784ae 000efaf Author: lisi <lisi@123.com> Date: Thu Apr 25 11:24:13 2019 +0800 update them commit tru24ae4162639ff01346cc12c1d578bddb5a33e Author: lisi <lisi@123.com> Date: Thu Apr 25 11:23:00 2019 +0800 update continue
fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{0}: checkout: moving from test_local to test_qy ae3c43b (origin/test_local, test_local) HEAD@{1}: cherry-pick: member page change 01311f0 HEAD@{2}: checkout: moving from test_all to test_local fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{3}: merge test_qy: Fast-forward 3f625ee HEAD@{4}: checkout: moving from test_qy to test_all fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{5}: checkout: moving from test_all_0001 to dbg_qy_0001 3f625ee HEAD@{6}: checkout: moving from dbg_qy_0001 to dbg_all_0001 fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{7}: commit: member page change 3f625ee HEAD@{8}: checkout: moving from test_local to test_qy 01311f0 HEAD@{9}: cherry-pick: member strategy page style change 3a60b15 HEAD@{10}: pull: Fast-forward 84eee19 HEAD@{11}: checkout: moving from test_all to test_local
三、回退系列命令:
1、丢弃工作区的修改(此时还没有执行git add .)
git checkout -- "文件路径" 删除指定文件的修改,执行后工作区是干净的
2、丢弃暂存区的修改 (此时已经执行了git add .但是还没有执行git commit -m "")
要用的是git reset命令,步骤如下:
git log 查看commit历史信息,拿到最新一次提交的commit-id
git reset --hard/--soft/--mixed "xxx"(这里指拿到的commit-id)
git reset命令有三个可选的参数:
git reset --hard af4d64d4d5d51709a4a0af7db01e5f2a392f9a19
此命令执行后,工作区中的所有未提交的修改都会丢失,无论是否是你想要保留的部分
-- hard这个命令是很危险的,它是git里面少数几个会丢失信息的命令,虽然也可以通过git reflog来挽救,但最好还是不要使用此参数
git reset --soft af4d64d4d5d51709a4a0af7db01e5f2a392f9a19
此命令执行后,查看文件的状态并没有做任何改变,只是代码所作用的效果不显示了
git reset af4d64d4d5d51709a4a0af7db01e5f2a392f9a19 //默认参数为mixed
Unstaged changes after reset:
M index.html
此命令执行后,将暂存区的修改撤回到了工作区,即git add .之前的状态
git status //查看文件状态,修改已经被撤回到暂存区
On branch dbg_qy_0001
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: index.html
no changes added to commit (use "git add" and/or "git commit -a")
3、将提交到分支的修改撤回到工作区(即已经执行了git commit,但还没有git push)
git log //拿到想要撤销的修改前一次的提交id
git reset af4d64d4d5d51709a4a0af7db01e5f2a392f9a19
git status //查看状态看到提交到分支上的修改以已经撤回到工作区
On branch test_qy
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: src/index.vue
no changes added to commit (use "git add" and/or "git commit -a")
4、将push到远程的个人分支的修改撤回(已经执行了git push但是还未merge到公共的repository)
这种情况同样也可以使用git reset命令来回退,但是当你回退到某一个版本再次push的时候,由于本地的版本落后于远程的版本,此时需要使用git push -f强制推送。
注意这种情况是只有你一个人使用的远程分支
5、要回退的代码已被merge(合入)到公共的repository
git log //查看提交历史信息,拿到要撤回的那次提交的commit-id
git revert "xxx"(commit-id)
若产生冲突,手动解决冲突,然后提交;
git revert 与 git reset的区别:
git reset执行后,要回退的那次提交之后的所有提交都被撤回到工作区,如果其他提交是团队其他开发人员做的,那么就会影响到其他人的代码,这样做是不合理的。
git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销
作为一次最新的提交
* git revert HEAD 撤销前一次 commit
* git revert HEAD^ 撤销前前一次 commit
* git revert commit (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。
git revert是提交一个新的版本,将需要revert的版本的内容再反向修改回去,
四、删除命令:
rm "文件"
rm test.txt