【问题标题】:Git: How to check whether local branch is ahead of remote branch when working directory is modified?Git:修改工作目录时如何检查本地分支是否领先于远程分支?
【发布时间】:2013-07-10 13:31:20
【问题描述】:

通常,当工作目录干净时,我可以使用“git status”。输出如下:

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)

但是,如果工作目录被修改,“git status”输出:

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   body/chap1.tex
#
no changes added to commit (use "git add" and/or "git commit -a")

第二个结果没有显示我是否已将所有更改推送到远程分支。有时,当工作目录被修改时,我仍然想要第一个结果。这种情况下怎么弄?

【问题讨论】:

    标签: git git-remote remote-branch


    【解决方案1】:

    这是暂存树的目的。您可以暂存您想要提交的工作,然后按如下方式发送到 repo:

    git add path/to/your/file.php
    

    这将暂存一个文件,然后将其存储在暂存树中(与您已提交的工作或未提交的工作分开),然后未来的git status 调用将向您显示您已暂存的工作与工作分开你没有承诺。这是 git 中的标准做法,可让您跟踪将要提交的内容。暂存是一种允许选择性提交的方法,但它应该符合您的目的。

    这里有一个链接可以更好地解释暂存区: http://git-scm.com/book/ch1-3.html

    【讨论】:

      【解决方案2】:

      也许最简单的方法是存储当前更改,检查然后弹出存储。因此,

      git stash
      git status
      git stash pop
      

      这不适用于所有更改,例如新文件(因为git stash 不会存储它们)。但是,我不得不说我没有重现您的问题:

      ebg@taiyo(14)$ git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 1 commit.
      #
      nothing to commit (working directory clean)
      ebg@taiyo(15)$ ed .gitignore 
      1165
      ...
      q
      ebg@taiyo(16)$ git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 1 commit.
      #
      # Changes not staged for commit:
      #   (use "git add <file>..." to update what will be committed)
      #   (use "git checkout -- <file>..." to discard changes in working directory)
      #
      #   modified:   .gitignore
      #
      no changes added to commit (use "git add" and/or "git commit -a")
      

      在上面,与origin 相关的状态仍然出现。不过,我的建议仍然有效:

      ebg@taiyo(17)$ git stash
      Saved working directory and index state WIP on master: b541ae8 Ignore 'SPOT Lang*' files
      HEAD is now at b541ae8 Ignore 'SPOT Lang*' files
      ebg@taiyo(18)$ git status
      # On branch master
      # Your branch is ahead of 'origin/master' by 1 commit.
      #
      nothing to commit (working directory clean)
      ebg@taiyo(19)$ git stash pop
      # On branch master
      # Your branch is ahead of 'origin/master' by 1 commit.
      #
      # Changes not staged for commit:
      #   (use "git add <file>..." to update what will be committed)
      #   (use "git checkout -- <file>..." to discard changes in working directory)
      #
      #   modified:   .gitignore
      #
      no changes added to commit (use "git add" and/or "git commit -a")
      Dropped refs/stash@{0} (8ff02f7a27053ca7680b0a98048542fcbe2bb440)
      

      【讨论】:

      • 我在 Windows 系统上使用 msys-git。有点不一样。
      猜你喜欢
      • 1970-01-01
      • 2018-09-12
      • 2017-12-04
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      • 2010-12-20
      • 2010-09-27
      • 2012-03-30
      相关资源
      最近更新 更多