1.在不提交当前分支的情况下切换到其它分支进行操作

假如在当前分支开发,但是还没有完成,不想提交,与此同时,另一个分支需要紧急修改代码,如果此时不提交当前分支的话,则会报错。

λ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
        README.md
Please commit your changes or stash them before you switch branches.
Aborting

此时可以用git stash命令,它会把所有未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工作目录。

执行之后,会将当前分支保存起来。

λ git stash
Saved working directory and index state WIP on console: ad10b85 third commit

可通过git stash list查看,执行之后,会显示你保存起来的当前commit id和信息

λ git stash list
stash@{0}: WIP on console: ad10b85 third commit

然后git checkout master就不会报错了

λ git checkout master
Switched to branch 'master'

在master分支编写完之后,切换回一开始的分支,继续编写未完成的部分,可以用git stash apply恢复之前保存的文件,但是stash里面的内容并未删除,用stash list还能看到,可以用git stash drop删除

E:\code\lhc\jmiss (console -> origin)
λ git stash apply
On branch console
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.md

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

E:\code\lhc\jmiss (console -> origin)
λ git stash list
stash@{0}: WIP on console: ad10b85 third commit

E:\code\lhc\jmiss (console -> origin)
λ git stash drop
Dropped refs/stash@{0} (fd48ff1ee180a3330df1f8636124a478a36d9839)

E:\code\lhc\jmiss (console -> origin)
λ git stash list

E:\code\lhc\jmiss (console -> origin)

当然还有更方便的命令,git stash pop,恢复之前隐藏的命令,并把stash里面的列表也删除了

2.返回以前提交过的版本

可以用git reflog查看以前提交的commit id

λ git reflog
f863a70 (HEAD -> console) HEAD@{0}: commit: fourth commit
ad10b85 HEAD@{1}: checkout: moving from master to console
cef5008 (master) HEAD@{2}: checkout: moving from console to master
ad10b85 HEAD@{3}: reset: moving to HEAD
ad10b85 HEAD@{4}: commit: third commit
8d01bbd HEAD@{5}: commit: second commit
b5002c7 HEAD@{6}: commit: first commit

git reset --hard <num> //回退到版本的commit id为num的版本

λ git reset --hard 8d01bbd
HEAD is now at 8d01bbd second commit

如果想要回退远程分支版本,则需要紧接着执行git push -f origin master,不加-f会报错,错误如下

λ git push lhc console
warning: redirecting to http://git.jd.com/lihongchao11/jdcloud.git/
To http://git.jd.com/lihongchao11/jdcloud
 ! [rejected]        console -> console (non-fast-forward)
error: failed to push some refs to 'http://git.jd.com/lihongchao11/jdcloud'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

强推则无问题

λ git push -f lhc console
warning: redirecting to http://git.jd.com/lihongchao11/jdcloud.git/
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for console, visit:
remote:   http://git.jd.com/lihongchao11/jdcloud/merge_requests/new?merge_request%5Bsource_branch%5D=console
remote:
To http://git.jd.com/lihongchao11/jdcloud
 + f863a70...8d01bbd console -> console (forced update)

直接退回到上一版本可以用这个命令:git reset --hard HEAD^,如果退回上两个版本,则^变为^^,多一个版本,多一个^,不过有时候会报如下错误,原因是:控制台中换行符默认是^,而不是\ ,所以它的more?的意思是问你下一行是否需要再输入,而^ 符号就被当做换行符而被git命令忽略掉了

λ git reset --hard HEAD^
More?
More?
fatal: ambiguous argument 'HEAD
': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

因此加上引号便可以避免这种问题,git reset --hard "HEAD^" 

λ git reset --hard "HEAD^"
HEAD is now at 8d01bbd second commit

3.撤销操作

如果不小心将修改的文件git add .到了暂存区域当中,可以采用git reset HEAD <file> 来取消暂存区域的文件

λ git status
On branch console
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README.md


E:\code\lhc\jmiss (console -> origin)
λ git reset HEAD README.md
Unstaged changes after reset:
M       README.md

E:\code\lhc\jmiss (console -> origin)
λ git status
On branch console
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.md

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

此时此文件已经不在暂存区,回到了刚刚被修改的状态,如果对刚才的修改也要取消,回到版本最初的状态呢?可以才用git checkout -- <file> 来取消文件修改,不过一旦取消,刚刚的修改就找不回来了,所以比较危险,因此使用之前确保修改没用。

λ git status
On branch console
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.md

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

E:\code\lhc\jmiss (console -> origin)
λ git checkout -- README.md

E:\code\lhc\jmiss (console -> origin)
λ git status
On branch console
nothing to commit, working tree clean

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐