博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git 常用的几剂后悔药
阅读量:4228 次
发布时间:2019-05-26

本文共 15426 字,大约阅读时间需要 51 分钟。

在 git add 之前,放弃对文件的所有修改

就算你用 rm 删除了也无所谓,照样还原回来。(如果是没有用 git 进行跟踪的文件,可不要轻易这么尝试哟!)

git checkout --

当你对一个文件进行了修改,还没有 add 进暂存区,你想要放弃所有的修改,将文件恢复到没有修改之前的样子,这时候可以使用 git checkout -- ,这条命令会将文件恢复到上一次 commit 的时候这个文件的样子。

[root@master GitTest]# vim hello.txt [root@master GitTest]# git statusOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")[root@master GitTest]# git checkout -- hello.txt [root@master GitTest]# git statusOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean

git restore  (推荐使用)

貌似新版本的 git restore  和 git checkout -- 差不多,以后可能用 git restore 更多一些了,毕竟见文知意,而且命令还短。

[root@master GitTest]# vim hello.txt [root@master GitTest]# git statusOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")[root@master GitTest]# git restore hello.txt[root@master GitTest]# git statusOn branch masterYour branch is up to date with 'origin/master'.nothing to commit, working tree clean

将 git add 的文件变成 git add 之前的状态

git restore --staged

[looking@master GitTest]$ vim b.txt[looking@master GitTest]$ git add b.txt[looking@master GitTest]$ git statusOn branch masterYour branch is up to date with 'origin/master'.Changes to be committed:  (use "git restore --staged 
..." to unstage) modified: b.txt[looking@master GitTest]$ git restore --staged b.txt[looking@master GitTest]$ git statusOn branch masterYour branch is up to date with 'origin/master'.Changes not staged for commit: (use "git add
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: b.txtno changes added to commit (use "git add" and/or "git commit -a")

git reset HEAD

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ vi myGit.txt # 添加新行 hello worldAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git add myGit.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD 
..." to unstage) modified: myGit.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git reset HEAD myGit.txtUnstaged changes after reset:M myGit.txtAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits)Changes not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: myGit.txtno changes added to commit (use "git add" and/or "git commit -a")

将 git commit 的文件变为 git add 之前的状态

git restore -s HEAD~1

执行以后,git commit 的 log 信息并没有丢失,你可以重新修改文件,然后 git add ,再 git commit --amend 对修改重新进行提交。

[looking@master GitTest]$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)nothing to commit, working tree clean[looking@master GitTest]$ git restore -s HEAD~1 hello.txt[looking@master GitTest]$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: hello.txtno changes added to commit (use "git add" and/or "git commit -a")

重新填写 git commit 信息

git commit --amend

有时候我们在提交完成之后才发现有几个文件没有提交,或者发现提交信息填写错了,这时候可以使用 git commit --amend 尝试重新进行提交。

$ git commit --amend- mv test.txt to test directory+ mv test.txt to test dir# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date:      Wed Jul 29 22:28:31 2020 +0800## On branch master# Your branch is ahead of 'origin/master' by 2 commits.#   (use "git push" to publish your local commits)## Changes to be committed:#       renamed:    test.txt -> test/test.txt#$ git commit --amend[master db41223] mv test.txt to test dir Date: Wed Jul 29 22:28:31 2020 +0800 1 file changed, 1 insertion(+) rename test.txt => test/test.txt (71%)

不想让 git 继续跟踪某个文件

git rm --cached

如果你只是想从暂存区删除文件,但是工作区的文件保持不变(将文件保存在磁盘),也就是说将文件保存在磁盘但是不想让 Git 进行跟踪,使用如下命令即可 git rm --cached :

Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ vi test.txt  # 新行添加 hello worldgitAdministrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: test.txtno changes added to commit (use "git add" and/or "git commit -a")Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git rm --cached test.txtrm 'test.txt'Administrator@PC-20200713AJJH MINGW64 /d/MyProject/Python/GitTest (master)$ git status # 显示文件 test.txt 为 Untracked filesOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)Changes to be committed: (use "git reset HEAD
..." to unstage) deleted: test.txtUntracked files: (use "git add
..." to include in what will be committed) test.txt

撤销最近的 commit,但是不撤销对应 commit 所做的文件修改

git reset HEAD~

注:使用此命令,你原来提交的代码都在,不会被撤销,也即只会撤销  commit 记录(不对文件内容进行修改)。

[root@master GitTest]# git status# On branch masternothing to commit, working directory clean[root@master GitTest]# vim b.txt # 删除了文件最后一行# 下面 commit 失败,因为修改的文件还没添加到暂存区[root@master GitTest]# git commit -m "delete last line in b.txt"# On branch master# Changes not staged for commit:#   (use "git add 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: b.txt#no changes added to commit (use "git add" and/or "git commit -a")------------------------------------------------------------[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "delete last line in b.txt"[master 131c2e9] delete last line in b.txt 1 file changed, 1 deletion(-)[root@master GitTest]# git logcommit 131c2e9e676726168a87db678c17e8ec404b8c4eAuthor: looking
Date: Sat Aug 8 01:35:09 2020 +0800 delete last line in b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt------------------------------------------------------------# 注意啦,放大招啦。[root@master GitTest]# git reset HEAD~Unstaged changes after reset:M b.txt# 这个时候回到了 add 和 commit 提交之前,但是在文件 b.txt 修改之后的那个状态[root@master GitTest]# git status# On branch master# Changes not staged for commit:# (use "git add
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: b.txt#no changes added to commit (use "git add" and/or "git commit -a")------------------------------------------------------------# 因为 commit 被撤销了,所以我们再重新提交一次[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "delete last line in b.txt"[master 33796e9] delete last line in b.txt 1 file changed, 1 deletion(-)[root@master GitTest]# git logcommit 33796e9844af4b6e1ee23c2d98ec5be4c5fef3e6Author: looking
Date: Sat Aug 8 01:44:27 2020 +0800 delete last line in b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txtcommit cd3e33625578c3aaf6f19b69c878559a1dcf552aMerge: 640fd3c 3fceb8a[root@master GitTest]#

撤销最近的 commit,同时撤销对应 commit 所作的文件修改

git reset --hard HEAD^1

注:使用之后,你最新的 commit 命令下修改的内容将完全被撤销,最近一次的 commit 记录也没了。

[root@master GitTest]# git add b.txt[root@master GitTest]# git commit -m "modify file b.txt"[master be7abc1] modify file b.txt 1 file changed, 2 deletions(-)[root@master GitTest]# git logcommit be7abc12a1a94e9390bd6c7f4bc747f9bbe86ab9Author: looking 
Date: Sat Aug 8 02:01:32 2020 +0800 modify file b.txtcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt------------------------------------------------------------ [root@master GitTest]# cat b.txt hahahahahahohohohohorebase testadd one line in b.txt by user Badd one line in b.txt by user B again------------------------------------------------------------ [root@master GitTest]# git reset --hard HEAD^1HEAD is now at f3b4d4a This is a combination of 2 commits.------------------------------------------------------------ # 注意看,git reset --hard HEAD^1 执行前后, b.txt 文件内容发生变化了哟!# git log 的最近一次 commit 没有了!------------------------------------------------------------[root@master GitTest]# cat b.txt hahahahahahohohohohoheheheheherebase testadd one line in b.txt by user Badd one line in b.txt by user B againhello world------------------------------------------------------------ [root@master GitTest]# git logcommit f3b4d4abd65a59c8a03df6caccf28f33422b4614Author: looking
Date: Tue Aug 4 21:42:27 2020 +0800 This is a combination of 2 commits. add hello in new line of looking.txt add world in new line of looking.txt

修改倒数二个 commit 相关内容

git rebase -i HEAD~2 和 git rebase --continue。

git rebase -i HEAD~2

git rebase -i HEAD~2 以后,git 会自动给你切换到下面这个界面,你将你需要修改的那个 commit 前边的 pick 修改为 edit。

[root@master GitTest]# git rebase -i HEAD~2edit 3d87922 nice to meet youpick df5f952 nice to meet you too# Rebase 3f20612..df5f952 onto 3f20612 (2 commands)## Commands:# p, pick 
= use commit# r, reword
= use commit, but edit the commit message# 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# x, exec
= run command (the rest of the line) using shell# b, break = stop here (continue rebase later with 'git rebase --continue')# d, drop
= remove commit# l, label

然后保存退出,而且 git 也已经提醒你版本已经 stopped 在这个 commit 节点了,你可以开始你的表演(修改)了:

[root@master GitTest]# git rebase -i HEAD~2Stopped at 3d87922...  nice to meet youYou can amend the commit now, with  git commit --amend Once you are satisfied with your changes, run  git rebase --continue

假如我想把原来插入的 nice to meet you 修改为 nice。用 vim 修改后用 git commit --amend 重新提交 commit。

[root@master GitTest]# vim hello.txt[root@master GitTest]# git add hello.txt [root@master GitTest]# git commit --amendnice # Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## Date:      Mon Aug 31 21:16:51 2020 +0800## interactive rebase in progress; onto 3f20612[detached HEAD bcbfd91] nice Date: Mon Aug 31 21:16:51 2020 +0800 1 file changed, 1 insertion(+)

git rebase --continue

然后继续 git rebase --continue 就好了(如果没有冲突,直接 git rebase --continue 也是极好的)。

[root@master GitTest]# git add hello.txt [root@master GitTest]# git rebase --continue[detached HEAD 72017cd] nice to meet you too 1 file changed, 1 insertion(+)Successfully rebased and updated refs/heads/master.

git rebase --abort

这个是你在 git rebase 过程当中的后悔药,如果你在 rebase 的任何过程中想退出 rebase 过程,直接执行 git rebase --abort 就直接退出回到 rebase 之前的状态啦。

将代码切换回之前的某个稳定版本(不乐意的话还可以切换回来)

git checkout  和 git checkout master。

[root@master GitTest]# git checkout 7187b0c677457b8422aaa3f52cbae35b3f397fccNote: switching to '7187b0c677457b8422aaa3f52cbae35b3f397fcc'.You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them, and you can discard any commits you make in thisstate without impacting any branches by switching back to a branch.If you want to create a new branch to retain commits you create, you maydo so (now or later) by using -c with the switch command. Example:  git switch -c 
Or undo this operation with: git switch -Turn off this advice by setting config variable advice.detachedHead to falseHEAD is now at 7187b0c git pull request test[root@master GitTest]# git checkout master Previous HEAD position was 7187b0c git pull request testSwitched to branch 'master'Your branch is up to date with 'origin/master'.

丢弃本地所有未提交的修改

git stash 同时 git stash clear

针对本地所有修改了但是还没进行 git commit 提交的部分,即使已经使用了 git add,也仍然可以丢弃。

[root@master GitTest]# git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git restore --staged 
..." to unstage) deleted: b.txt modified: hello.txtChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git restore
..." to discard changes in working directory) modified: c.txt[root@master GitTest]# git stashSaved working directory and index state WIP on master: 2017dee add newline in c.txt[root@master GitTest]# git stash clear[root@master GitTest]# git statusOn branch masterYour branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits)nothing to commit, working tree clean

git stash 结合 git stash pop 还可以适用于快速对比程序修改前后的运行差异,而且可以在现有修改的基础上继续修改。 

丢弃本地所有修改,强制拉取覆盖本地

git fetch --all && git reset --hard origin/master

如果你想以远程仓库的版本内容为准,丢弃本地所有修改(当然,本地的 untracked file 不属于版本控制的范围,不会受到这条命令的影响),又不想删除本地仓库以后再次 git  clone,就按下边这么做吧。

git fetch --all && git reset --hard origin/master

删除本地仓库,重新克隆(终极解决方案)

rm -rf GitTest

git clone https://github.com/2392863668/GitTest

如果你想以远程仓库的版本内容为准,丢弃本地所有修改(包括仓库里边 untracked file 文件的改动),那就重新克隆一个吧。

[root@master workspace]# rm -rf GitTest[root@master workspace]# git clone https://github.com/2392863668/GitTestCloning into 'GitTest'...remote: Enumerating objects: 26, done.remote: Counting objects: 100% (26/26), done.remote: Compressing objects: 100% (19/19), done.remote: Total 247 (delta 11), reused 15 (delta 5), pack-reused 221Receiving objects: 100% (247/247), 1.27 MiB | 869.00 KiB/s, done.Resolving deltas: 100% (95/95), done.

转载地址:http://mjjqi.baihongyu.com/

你可能感兴趣的文章
UVM:8.2.3 复杂的重载
查看>>
UVM:8.2.4 factory 机制的调试
查看>>
UVM:8.3.1 重载transaction
查看>>
UVM:8.3.2 重载sequence
查看>>
leetcode171.[math] Excel Sheet Column Number
查看>>
Log4j配置
查看>>
java发送https请求证书问题
查看>>
js新消息提醒
查看>>
js窗体消息提醒
查看>>
深入Hibernate映射文件(二)——<hibernate-mapping>的属性
查看>>
详解在Spring中进行集成测试
查看>>
Struts2中过滤器和拦截器的区别
查看>>
51单片机:led灯闪烁10次后熄灭
查看>>
安卓:okhttp请求,获取返回数据
查看>>
安卓:股票筛选及分析系统
查看>>
增加windows下Tomcat运行时的内存
查看>>
tomcat群集中session共享的几个方案
查看>>
查找google谷歌北京IP地址的方法
查看>>
本科大数据专业该怎么上?
查看>>
云创大数据1+X大数据应用部署与调优职业技能等级证书预申报正式开启!
查看>>