我是Git新手。我最近把一个Rails项目从Subversion转移到Git。我遵循了这里的教程:http://www.simplisticcomplexity.com/2008/03/05/cleanly-migrate-your-subversion-repository-to-a-git-repository/

我还使用unfuddle.com来存储我的代码。我在上班/下班的火车上对我的Mac笔记本电脑进行更改,然后在我有网络连接时,使用以下命令将它们推到不稳定状态:

git push unfuddle master

我使用Capistrano进行部署,并使用主分支从unduddle存储库中提取代码。

最近,当我在笔记本电脑上运行“git status”时,我注意到下面的消息:

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

我很困惑为什么。我以为我的笔记本电脑是源头…但不知道是否我最初从Subversion或推送到unduddle是导致消息显示的原因。我怎样才能:

找出Git认为“源/主”在哪里? 如果是在其他地方,我如何把我的笔记本电脑变成“源/主”? 让这条信息消失。这让我觉得Git对某些事情不满意。

我的mac运行的是Git 1.6.0.1版本。


当我按照dbr的建议运行git remote show origin时,我得到以下结果:

~/Projects/GeekFor/geekfor 10:47 AM $ git remote show origin
fatal: '/Users/brian/Projects/GeekFor/gf/.git': unable to chdir or not a git archive
fatal: The remote end hung up unexpectedly

当我按照Aristotle Pagaltzis的建议运行git remote -v时,我得到以下结果:

~/Projects/GeekFor/geekfor 10:33 AM $ git remote -v
origin  /Users/brian/Projects/GeekFor/gf/.git
unfuddle    git@spilth.unfuddle.com:spilth/geekfor.git

现在,有趣的是,我在geekfor目录中处理我的项目,但它说我的原点是gf目录中的本地机器。我相信gf是我在将项目从Subversion转换到Git时使用的临时目录,可能也是我从那里推送到unduddle的地方。然后我相信我检查了一个新的副本从unuddle到geekfor目录。

所以看起来我应该遵循dbr的建议,这样做:

git remote rm origin
git remote add origin git@spilth.unfuddle.com:spilth/geekfor.git

当前回答

有时,本地缓存的origin master (origin/master)版本与真正的origin master之间存在差异。

如果你运行git远程更新,这将使origin master与origin/master重新同步

请看这个问题的公认答案

git pull origin master和git pull origin/master之间的差异

其他回答

在您自己的提交发生之前,可以将其重置为特定的提交。

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
#
nothing to commit (working directory clean)

使用git log来查找在本地更改发生之前的提交。

$ git log
commit 3368e1c5b8a47135a34169c885e8dd5ba01af5bb
...
commit baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e
...

注意本地提交,并直接重置到之前的提交:

git reset --hard baf8d5e7da9e41fcd37d63ae9483ee0b10bfac8e

【解决方案】

$ git push origin

这为我解决了问题。 它做了什么,它同步我的主(在笔记本电脑上)与“原点”,这是在远程服务器上。

它在等着你去“推”。试一试:

$ git push

我最近遇到了这个问题,我想这是因为我删除了一些我不再需要的文件。问题是git不知道文件已经被删除了,它看到服务器上还有文件。(server = origin)

所以我跑了

git rm $(git ls-files --deleted)

然后运行commit和push。

这就解决了问题。

我正在与这个问题作斗争,之前的答案都没有解决我所看到的问题。我已经把问题还原到最基本的部分,看看能不能把问题说清楚。

我创建了一个新的存储库(rep1),放入一个文件并提交。

mkdir rep1
cd rep1
git init
echo "Line1" > README
git add README
git commit -m "Commit 1"

我创建了rep1的克隆,并将其称为rep2。我查看了rep2内部,看到文件是正确的。

cd ~
git clone ~/rep1 rep2
cat ~/rep2/README

在rep1中,我对文件进行了单个更改并提交。然后在rep1中,我创建了一个指向rep2的远程并推动更改。

cd ~/rep1
<change file and commit>
git remote add rep2 ~/rep2
git push rep2 master

现在,当我进入rep2并执行'git status'时,我被告知我在origin之前。

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   README
#

rep2中的README与第二次提交之前的README相同。我所做的唯一修改是rep1,我想做的就是把它们推到rep2。我没有抓住什么?