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 & ...
随机推荐
- Mac下PHP环境的搭建
Mac下PHP环境的搭建 目录 Mac下PHP环境的搭建(基于XAMPP) phpmyadmin Mac下PHP环境的搭建(基于XAMPP) 下载XAMPP的Mac版 启动Apache Web Ser ...
- js中的命名规范
在实际开发中规范的命名,不仅方便自己查看,理解变量的实际意义,而且在团队开发中也能提高开发效率. 下面将介绍javascript中的变量的命名规范: 1)首先,变量名要有实际意义,不建议使用单个的字母 ...
- C#中怎么在EXCEL中的单元格中画斜线啊 ??
Code Snippet 做法: 1,先添加引用COM,找 Excel 2,using Excel = Microsoft.Office.Interop.Excel; 3, 代码 private Ex ...
- Input框去掉蓝色边框
Input框去掉蓝色边框: <input type="text" name="" value="" class="Inpt& ...
- 多线程的练习----妖,等待唤醒,代码重构,lock到condition
× 目录 [1]需求 [2]妖的出现和解决 [3]等待唤醒 [4]代码重构 [5]改成Lock Condition ------------------------------------- 1,需求 ...
- 每天一道LeetCode--344. Reverse String
Write a function that takes a string as input and returns the string reversed. Example:Given s = &qu ...
- Android之天气APP
做一个天气信息APP,通过读取公开发布的气象数据,提供实时天气更新,帮助用户时刻了解天气情况.通过APP可以查询到自己所处位置的天气预报,也可以通过定位系统,获取省内任意位置的气象环境信息. API接 ...
- MVC常用 ActionResult
1.EmptyResult 2.ContentResult 3.FileResult 4.JavaScriptResult 5.JsonResult 6.HttpStatusCodeResult 7. ...
- Google学术指数2015版
除了影响因子,还有许多指标可以评价论文价值,如Google的H5指数H5中位数.现在JCR 的2015影响因子早已放出.Google也于6月份提供了其最新的2015学术指数.2015版的学术指数,是基 ...
- 【原创】如何找到Oracle中哪条记录被锁
通常有这种情况,某个表或者准确的说是表的某条记录被锁(TX锁),在业务层面排查之余,一般都会想知道是哪条记录被锁,每次被锁的是否是同一条记录?还是每次都不同?通过记录可以找到这条记录可以在哪个模块.哪 ...