博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第一次使用git,撤销git add操作 (undo git add before commit)
阅读量:4038 次
发布时间:2019-05-24

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

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 

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:

  1. cd to my great new project directory to try out git, the new hotness
  2. git init
  3. git add .
  4. git status

    ... lots of crap scrolls by ...

    => Damn, I didn't want to add all of that.

  5. google "undo git add"

    => find Stackoverflow - yay

  6. 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 .

    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 
..." 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)

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

你可能感兴趣的文章
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>