NOTE:

Following content is directly reprinted from http://andrewberls.com/blog/post/git-tricks-unstaging-files, please go to the original website for more details.

-----------------------------------------------------------

Git tricks: Unstaging files

Author: Andrew Berls, 6 May 2013

This post dives a bit into the git reset command. If you want to jump straight to the good stuff, clickhere.

I wanted to share a handy alias I use for removing files from the staging area in git. Often I'll be working and adding files to the staging area with git add, and then decide (for example), that I don't want to commit some files with the others and get them out of the staging area (but keep my work intact). Let's look at an example case - running git status after staging a file might look like this:

$ git add example.txt

$ git status
# On branch test
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   modified:   example.txt
#

Hey, that's neat - it tells us right there how to unstage files! This is accomplished using the git resetcommand. From the docs:

git reset [-q] [<commit>] [--] <paths>…
This form resets the index entries for all <paths> to their state at <commit>. 
(It does not affect the working tree, nor the current branch.)
 
This means that git reset <paths> is the opposite of git add <paths>.

 

git reset can be used for several things -it can point the current HEAD at a specified state, and we'll be using it to copy entries from HEAD to the index, or staging area, thus removing our file from the staging area and leaving our working tree unchanged. Also important is that <commit>defaults to HEAD, so we can leave that bit out. Let's give it a shot!

$ git reset -- example.txt
Unstaged changes after reset:
M   example.txt
$ git status
# On branch test
# Changes not staged for commit:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#   modified:   example.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

And our changes are left intact but removed from the staging area! Mission accomplished! However, you might be wondering about the strange -- that shows up in the command. Those are referred to as the 'bare double dashes', and they prevent ambiguity by ensuring that we're resetting a file and not specifying a branch. For example, if you have a branch and a file both named example and you run git reset example, you might see the following:

fatal: ambiguous argument 'example': both revision and filename
Use '--' to separate filenames from revisions

Git can't tell if you're talking about the branch or the file (each of which has very different consequences), so the -- makes it clear we're not talking about a branch and that we want to reset our file.

So we've seen that we can use git reset to unstage our files. However, it'd be nice to be able to say git unstage <paths>, so let's define that using a git alias:

git config --global alias.unstage 'reset --'

And there you have it! Now we can run git unstage example.txt to our heart's content.

Git tricks: Unstaging files的更多相关文章

  1. git add Untracked files

    git add *  将目录里的所有文件提交到暂存区后 git status 查看状态  所有文件都是绿色的表示本地的文件和暂存区的文件是一样的 然后在本地修改一个文件 然后新建一个文件 在使用git ...

  2. git中Untracked files如何清除

    $ git status # On branch test # Untracked files: # (use "git add <file>..." to inclu ...

  3. git 提交ignore files

    1,首先在命令行创建.gitignore文件 $ touch .gitignore 2,在文件.gitignore 加入要忽略的文件入 $ echo *.class > .gitignore 3 ...

  4. git Staging Deleted files

    Use git rm foo to stage the file for deletion. (This will also delete the file from the file system, ...

  5. git subtree 使用

    这个是备忘录.原网页(https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec , http://cncc.bingj.co ...

  6. GIT分布式版本控制系统

    Git诞生历史 我想大家还记得Linus torvalds在1991年时发布了Linux操作系统吧,从那以后Linux系统变不断发展壮大,因为Linux系统开源的特性,所以一直接受着来自全球Linux ...

  7. Git Learning - By reading ProGit

    Today I begin to learn to use Git. I learn from Pro Git. And I recommend it which is an excellent bo ...

  8. 一张图看懂git push

    基本用法 上面的四条命令在工作目录.暂存目录(也叫做索引)和仓库之间复制文件. git add files 把当前文件放入暂存区域. git commit 给暂存区域生成快照并提交. git rese ...

  9. [Git] Git基础

    远程仓库 查看远程仓库: git remote -v 添加远程仓库: git remote add <repoName> <url> 拉取远程仓库数据: git fetch & ...

随机推荐

  1. MemCache超详细解读 图

    http://www.cnblogs.com/xrq730/p/4948707.html   MemCache是什么 MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于 ...

  2. error C2440:“类型转换":无法从“void (__thiscall Play1::* )(int *)”转换为“cocos2d::SEL_CallFucN

    转自:http://zhidao.baidu.com/link?url=VfSSkA0xfTVwNKaKh4tqW_sXlcsK-Rb16nEtQw5zaq5_306lnwJN3Kdb-rFp-r4L ...

  3. 随机 I/O & 顺序 I/O

    在谈这俩概念前.先来说说 大I/O vs. 小I/O     通常.我们把 <=16KB 的I/O认为是小I/O.而 >=32KB 的I/O认为是大I/O     了解I/O的大小.影响到 ...

  4. c++ 设计模式6 (Decorator 装饰模式)

    4. “单一职责”类模式 在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式代表: Decorato ...

  5. Cyclic Nacklace

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  6. [WinForm] VS2010发布、打包安装程序(超全超详细)

    1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1.“应 ...

  7. CentOS6.5 64bit 运行Mono程序

    前几日和一技术友聊天,认为转Java好,java可以在Linux下运行,貌似c#不可以哦,就做了个尝试,运行控制台程序和窗口程序(界面编程,Linux下Java好像也比较烦吧) 现在贴环境: 参考:C ...

  8. Android读取RAM,ROM,SD卡容量

    1)简介 一般人们在买手机的时候,在手机配置上都会出现"内存容量:512MB ROM+512MB RAM "等等类似这样的说明,可能很多人都知道RAM的意思就是运存的意思,但是对于 ...

  9. Oracle基础(九) Oracle的体系结构

    一.Oracle体系结构概述: Oracle的体系结构是指数据库的组成.工作过程与原理,以及数据在数据库中的组织与管理机制.要了解Oracle数据库的体系结构,必须理解Oracle系统的重要概念和主要 ...

  10. 1172 Hankson 的趣味题[数论]

    1172 Hankson 的趣味题 2009年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Descrip ...