git提交前撤销git add操作
http://stackoverflow.com/questions/348170/undo-git-add-before-commit
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 initgit 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 rm deletes 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 -r to recurse. sigh.
git rm -r --cached .
Okay, now I'm back to where I started. Next time I'm going to use -n to 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 --cached not destroying anything (and what if I misspelled it)
GIT 在提交之前撤销add操作
git 在提交之前撤销add操作
问题:
在使用git时,在未添加.ignore文件前使用 git add . 将所有文件添加到库中,不小心将一些不需要加入版本库的文件加到了版本库中。由于此时还没有提交所以不存在HEAD版本,不能使用 git reset HEAD命令。
解决:
使用 git rm -r --cached .
ps:注意最后是有点的。
---------------------------------------改写最后一次提交---------------------------------------
git commit -m'改写最后一次提交'
git add forgotten_file //补上忘记提交的文件
git commit --amend
----------------------------------------remote--------------------------------------------
git remote add [shortname] [url] //添加远程仓库
git remote -v //列出远程仓库
git remote show [remote-name] //列出远程仓库详细信息
git remote rename old-name new-name //远程仓库重命名
git remote rm [remote-name] //删除远程仓库
----------------------------------------fetch-----------------------------------------------
git fetch [remote-name] //将远端的数据拉到本地仓库,并不自动合并到当前分支,仍需手工合并。
---------------------------------------tag-------------------------------------------------------
git tag v1 //建立标签
git tag -a v1 -m '建立标签'
git show v1 //查看标签版本信息
----------------------------------linux 下自动补全功能------------------------------------
在git源码中 contrib/completion 目录中的 git -completion.bash 复制到自己的用户目录中。并把下面内容添加到你的 .bashrc文件中
source ~/.git-completion.bash
---------------------------------设置Git命令别名---------------------------------------------
git config --global alias.co checkout //设置checkout 命令别名
git config --global alias.br branch //设置branch 命令别名
git config --global alias.ci commit //设置commit 命令别名
git config --global alias.st status //设置status 命令别名
git config --global alias.last 'log -1 HEAD' //查看最后一次提交信息
git config --global alias.visual "!gitk" //启动gitk。运行外部命令,只需在命令前加上 ! 。

浙公网安备 33010602011771号