Git tricks: Unstaging files
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的更多相关文章
- git add Untracked files
git add * 将目录里的所有文件提交到暂存区后 git status 查看状态 所有文件都是绿色的表示本地的文件和暂存区的文件是一样的 然后在本地修改一个文件 然后新建一个文件 在使用git ...
- git中Untracked files如何清除
$ git status # On branch test # Untracked files: # (use "git add <file>..." to inclu ...
- git 提交ignore files
1,首先在命令行创建.gitignore文件 $ touch .gitignore 2,在文件.gitignore 加入要忽略的文件入 $ echo *.class > .gitignore 3 ...
- git Staging Deleted files
Use git rm foo to stage the file for deletion. (This will also delete the file from the file system, ...
- git subtree 使用
这个是备忘录.原网页(https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec , http://cncc.bingj.co ...
- GIT分布式版本控制系统
Git诞生历史 我想大家还记得Linus torvalds在1991年时发布了Linux操作系统吧,从那以后Linux系统变不断发展壮大,因为Linux系统开源的特性,所以一直接受着来自全球Linux ...
- 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 ...
- 一张图看懂git push
基本用法 上面的四条命令在工作目录.暂存目录(也叫做索引)和仓库之间复制文件. git add files 把当前文件放入暂存区域. git commit 给暂存区域生成快照并提交. git rese ...
- [Git] Git基础
远程仓库 查看远程仓库: git remote -v 添加远程仓库: git remote add <repoName> <url> 拉取远程仓库数据: git fetch & ...
随机推荐
- php与mysql的链接到底用mysql 还是mysqli,pdo
参考:http://php.net/manual/en/mysqlinfo.api.choosing.php
- 添加IFrame导致内存溢出的解决过程(IE浏览器,目前发现了原因,还未解决)
1. 现象 每次动态添加iframe时,iexplore.exe进程占据的内存都会增加(大概10M左右),不会自动释放,最终导致内存溢出 2. 解决过程 经过网络的一番搜索,基本上给出的解决方案是 ...
- LeetCode10 Regular Expression Matching
题意: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- iOS7开发中的新特性
iOS7到现在已经发布了有一段时间了.相信你现在已经了解了它那些开创性的视觉设计,已经了解了它的新的API,比如说SpirteKit,UIKit Dynamics以及TextKit,作为开发者 ...
- VS 之 InstallShield Limited Edition for Visual Studio 2015 图文教程
从Visual Studio 2012开始,微软就把自家原来的安装与部署工具彻底废掉了,转而让大家去安装使用第三方的打包工具“InstallShield Limited Edition for Vis ...
- Android 高级UI设计笔记14:Gallery(画廊控件)之 3D图片浏览
1. 利用Gallery组件实现 3D图片浏览器的功能,如下: 2. 下面是详细的实现过程如下: (1)这里我是测试性代码,我的图片是自己添加到res/drawable/目录下的,如下: 但是开发中不 ...
- 菜菜菜鸟学习之JavaWeb 入门1(自己的学习理解,不对之处请大神们多多指教啊)
一.相关基础知识 1.C/S(Client/Server)架构和B/S(Browser/Server)架构 首先说C/S架构,简单讲其实很常见,类似QQ等需要下载客户端的应用程序就是建立在C/S架构中 ...
- 【LeetCode 1】算法修炼 --- Two Sum
Question: Given an array of integers, find two numbers such that they add up to a specific target nu ...
- 《算法导论》习题解答 Chapter 22.1-6(求universal sink 通用汇点)
思路:设置两个游标i指向行,j指向列,如果arr[i][j]==1,则i=max{i+1,j},j++:如果arr[i][j]==0,则j=max{i+1,j+1}. 伪代码: has_univers ...
- MySQL中MyISAM引擎及InnoDB引擎的缓存优化设计
MyISAM引擎中,为了提高io效率以及读取效率,将对磁盘频繁读取的索引数据加载至内存中操作. MyISAM设计了一个在存放在内存中的索引缓冲池Key Cache.Key Cache只缓存索引数据,通 ...