git 撤销挂起的更改_如何执行TFS等效的'撤消挂起的更改'

bd96500e110b49cbb3cd949968f18be7.png

How do I perform the equivalent of the TFS 'Undo pending changes' in Git, on one or multiple files?

That basically means to do these steps:

Undo changes on disk

Resetting any changes Git has discovered

Getting the latest changes on the file from Git

It would be good to know the differences (if there are any) in commands for doing this if you've (1) just changed it on disk, without adding it, but also when you've (2) done the add-command and for a bonus, (3) even when you have commit the change.

解决方案

For 1 and 2, all you need to do is:

git stash -u #same effect as git reset --hard, but can be undone

this will throw away any changes. Be careful if you use reset. Read up on manipulating the index and the permutations of the hard, soft and mixed options with the reset and checkout. The progit book explains this in detail: http://progit.org/2011/07/11/reset.html

For 3,

git reset --hard HEAD^

but would be better to issue a git stash -u before this - just in case you have pending changes.

This will reset the current branch to the parent of the current commit. Look up "tree-ish" online. ^ and ~N after a reference will allow you to point to any reachable points in the history of that reference. To understand how history is tracked in git, "Git for computer scientists" explains the Directed Acyclic Graph well: http://eagain.net/articles/git-for-computer-scientists/

To get individual files from the state of the current commit (ie, throw away changes), you can use checkout

git checkout HEAD --

If you issued the last reset command above in error, you're not in trouble. Git keeps track of where the branches used to point in the reflog.

git reflog

will list you the history. You can see in that output how to reference each, so:

git reset --hard HEAD@{1}

will reset the branch to where it used to be 1 change before.

To add, if you want to wipe ignored files and untracked files, you can wipe with this:

git clean -xdf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值