git 状态(无需提交,工作目录干净),但已提交更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37269222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:10:38  来源:igfitidea点击:

git status (nothing to commit, working directory clean), however with changes commited

gitgithubgit-commit

提问by ivanleoncz

I found many questions with similar subject, but I didn't found any practical guidance about this issue: why git statusinforms me nothing to commit, working directory clean, even tough I have made a modification at my local branch?

我发现了很多类似主题的问题,但我没有找到关于这个问题的任何实际指导:为什么要git status通知我nothing to commit, working directory clean,即使我在当地分支机构进行了修改?

Here are the steps which I followed:

以下是我遵循的步骤:

  • git init[On branch master - Initial commit, nothing to commit (create/copy files and use "git add" to track)]
  • git remote add https://github.com/username/project.git
  • git pull origin master
  • touch test
  • git add test
  • git commit -m "Adding file for test purposes only."
  • git status[On branch master - nothing to commit, working directory clean]
  • git init[在分支 master - 初始提交,无需提交(创建/复制文件并使用“git add”进行跟踪)]
  • git remote add https://github.com/username/project.git
  • git pull origin master
  • touch test
  • git add test
  • git commit -m "Adding file for test purposes only."
  • git status[在 master 分支上 - 无需提交,工作目录干净]

If I do a git push, the modification is committed to the remote branch. I just want to perform "git status" after my modifications, and receive the information that I have changes on my local branch that must be pushed to the remote branch of the project.

如果我执行 a git push,则修改将提交到远程分支。我只是想在我修改后执行“git status”,并接收我在本地分支上有更改必须推送到项目的远程分支的信息。

Can someone tell me what's going? Straight to the point, please.

有人能告诉我这是怎么回事吗?请说到点子上。

Thanks in advance SO community!

在此先感谢 SO 社区!

回答by Chris Maes

Your local branch doensn't know about the remote branch. If you don't tell git that your local branch (master) is supposed to compare itself to the remote counterpart (origin/masterin this case); then git status won't tell you the difference between your branch and the remote one. So you should use:

您的本地分支不知道远程分支。如果你不告诉 git 你的本地分支(master)应该将自己与远程分支(在这种情况下为origin/master)进行比较;那么 git status 不会告诉你你的分支和远程分支之间的区别。所以你应该使用:

git branch --set-upstream-to origin/master

or with the short option:

或使用短选项:

git branch -u origin/master

This options --set-upstream-to(or -uin short) was introduced in git 1.8.0.

这个选项--set-upstream-to(或简称-u)是在 git 1.8.0 中引入的。

Once you have set this option; git statuswill show you something like:

一旦你设置了这个选项;git status将向您展示以下内容:

# Your branch is ahead of 'origin/master' by 1 commit.

回答by dubes

git statusoutput tells you three things by default:

git status默认情况下,输出会告诉您三件事:

  1. which branch you are on
  2. What is the status of your local branch in relation to the remote branch
  3. If you have any uncommitted files
  1. 你在哪个分支
  2. 您的本地分支相对于远程分支的状态是什么
  3. 如果您有任何未提交的文件

When you did git commit, it committed to your local repository, thus #3 shows nothing to commit, however, #2 should show that you need to push or pull if you have setup the tracking branch.

当你这样做时git commit,它提交到你的本地存储库,因此#3 显示没有提交,但是,如果你设置了跟踪分支,#2 应该表明你需要推送或拉取。

If you find the output of git status verbose and difficult to comprehend, try using git status -sbthis is less verbose and will show you clearly if you need to push or pull. In your case, the output would be something like:

如果您发现 git status 的输出冗长且难以理解,请尝试使用git status -sb它不那么冗长,并且会清楚地显示您是否需要推或拉。在您的情况下,输出将类似于:

master...origin/master [ahead 1]

主...起源/主 [前面 1]

git statusis pretty useful, in the workflow you described do a git status -sb: after touching the file, after adding the file and after committing the file, see the difference in the output, it will give you more clarity on untracked, tracked and committed files.

git status非常有用,在您描述的工作流程中执行以下操作git status -sb:触摸文件后,添加文件后和提交文件后,查看输出中的差异,它将使您更清楚地了解未跟踪、已跟踪和已提交的文件。

Update #1
This answer is applicable if there was a misunderstanding in reading the git status output. However, as it was pointed out, in the OPs case, the upstream was not set correctly. For that, Chris Mae's answeris correct.

更新 #1
如果在阅读 git status 输出时存在误解,则此答案适用。但是,正如有人指出的那样,在 OP 的情况下,上游设置不正确。为此,Chris Mae 的回答是正确的。

回答by Jonathan

The problem is that you are not specifying the name of the remote: Instead of

问题是您没有指定遥控器的名称:而不是

git remote add https://github.com/username/project.git

you should use:

你应该使用:

git remote add origin https://github.com/username/project.git