在commit之前撤销git add操作 (undo git add before commit)
2012-03-15 22:51
495 查看
http://stackoverflow.com/questions/348170/undo-git-add-before-commit
I mistakenly added files using the command
I have not yet
run
Is there a way to undo this or remove these files from the commit?
You want:
Reasoning:
Also a newbie I first tried
(to undo my entire initial add) only to get this (not so) helpful message:
turns out that this is because the HEAD ref (branch?) doesn't exist until after the first commit. That is, you'll run into the same newbie problem as me if your workflow, like mine, was something like:
cd to my great new project directory to try out git, the new hotness
... lots of crap scrolls by ...
=> Damn, I didn't want to add all of that.
google "undo git add"
=> find Stackoverflow - yay
=> fatal: Failed to resolve 'HEAD' as a valid ref.
it further turns out that there's a bug logged against the unhelpfulness of this in
the mailing list.
And that the correct solution was right there in the git status output (which, yes, I glossed over as 'crap)
And the solution indeed is to use
Note the warnings elsewhere here -
you use --cached. Here's the result of git help rm`:
--cached Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left.
I proceed to use
to remove everything and start again. Didn't work though, because while
recurse. sigh.
Okay, now I'm back to where I started. Next time I'm going to use
do a dry run and see what will be added:
UPDATE Oh yeah forgot to mention - I zipped up everything to a safe place before trusting
destroying anything (and what if I misspelled it)
I mistakenly added files using the command
git add file
I have not yet
run
git commit.
Is there a way to undo this or remove these files from the commit?
You want:
git rm --cached <added_file_to_undo>
Reasoning:
Also a newbie I first tried
git reset .
(to undo my entire initial add) only to get this (not so) helpful message:
fatal: Failed to resolve 'HEAD' as a valid ref.
turns out that this is because the HEAD ref (branch?) doesn't exist until after the first commit. That is, you'll run into the same newbie problem as me if your workflow, like mine, was something like:
cd to my great new project directory to try out git, the new hotness
git init
git add .
git status
... lots of crap scrolls by ...
=> Damn, I didn't want to add all of that.
google "undo git add"
=> find Stackoverflow - yay
git reset .
=> fatal: Failed to resolve 'HEAD' as a valid ref.
it further turns out that there's a bug logged against the unhelpfulness of this in
the mailing list.
And that the correct solution was right there in the git status output (which, yes, I glossed over as 'crap)
... # Changes to be committed: # (use "git rm --cached <file>..." to unstage) ...
And the solution indeed is to use
git rm --cached FILE
Note the warnings elsewhere here -
git rmdeletes your local working copy of the file, but not if
you use --cached. Here's the result of git help rm`:
--cached Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left.
I proceed to use
git rm --cached .
to remove everything and start again. Didn't work though, because while
add .is recursive, turns out
rmneeds
-rto
recurse. sigh.
git rm -r --cached .
Okay, now I'm back to where I started. Next time I'm going to use
-nto
do a dry run and see what will be added:
git add -n .
UPDATE Oh yeah forgot to mention - I zipped up everything to a safe place before trusting
git help rmabout the
--cachednot
destroying anything (and what if I misspelled it)
相关文章推荐
- 在commit之前撤销git add操作 (undo git add before commit)
- 在commit之前撤销git add操作 (undo git add before commit)
- 第一次使用git,撤销git add操作 (undo git add before commit)
- git在未提交commit之前,撤销回退所有操作
- GIT 在提交之前撤销add操作
- git 在提交之前撤销add操作
- git 在提交之前撤销add操作
- git未commit之前,撤销add文件的方法
- Git 在提交之前撤销add操作
- git 在提交之前撤销add操作
- Git 实际操作之一 - git init / git status / git add / git commit / git log
- 记录一次操作git 的愚蠢行为(本地代码只 git add 过,没有 commit ,push 过,然后版本回退 导致本地代码丢失)
- git---撤销 git add . 操作
- git add, commit, push如何撤销
- git撤销commit操作
- git 操作之合并commit(提交之前本地合并)
- git 撤销操作--git checkout、git reset、git revert、git commit -- amend
- Git的纯命令操作,Install,Clone , Commit,Push,Pull,版本回退,撤销更新,分支的创建/切换/更新/提交/合并,代码冲突
- 记录一次操作git 的愚蠢行为(本地代码只 git add 过,没有 commit ,push 过,然后版本回退 导致本地代码丢失)