Git:如何在修改工作目录时检查本地分支是否位于远程分支之前?

时间:2013-07-10 13:31:20

标签: git git-remote remote-branch

通常,当工作目录是干净的时,我可以使用“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")

如果我已将所有更改推送到远程分支,则第二个结果不会显示。有时,我仍然想要修改工作目录时的第一个结果。如何在这种情况下得到它?

2 个答案:

答案 0 :(得分:1)

这是登台树的目的。您可以暂存要提交的工作,然后按以下方式发送到仓库:

git add path/to/your/file.php

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

这是一个更好地解释暂存区域的链接: http://git-scm.com/book/ch1-3.html

答案 1 :(得分:1)

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

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)