zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    人人都有数字替身的时代马上到来
    教你如何在linux下查看服务是否已经启动或者关闭
    提前了解2019年物联网发展的六大趋势
    本科理工男如何学习Linux
    linux常见命令ps的应用
    useradd 命令的常见用法
    简单聊聊Linux学习经历
    什么是公网IP、内网IP和NAT转换?
    远程获得的有趣的linux命令
    js练习题之查找数组中的位子
  • 原文地址:https://www.cnblogs.com/cindy-hu-23/p/5105376.html
Copyright © 2011-2022 走看看