如何在 git 中找到 origin/master 的位置,以及如何更改它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/277077/
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
How can I find the location of origin/master in git, and how do I change it?
提问by Brian Kelly
I'm a Git newbie. I recently moved a Rails project from Subversion to Git. I followed the tutorial here: http://www.simplisticcomplexity.com/2008/03/05/cleanly-migrate-your-subversion-repository-to-a-git-repository/
我是一个 Git 新手。我最近将一个 Rails 项目从 Subversion 转移到 Git。我跟着这里的教程:http: //www.simplisticcomplexity.com/2008/03/05/cleanly-migrate-your-subversion-repository-to-a-git-repository/
I am also using unfuddle.com to store my code. I make changes on my Mac laptop on the train to/from work and then push them to unfuddle when I have a network connection using the following command:
我也在使用 unfuddle.com 来存储我的代码。我在上下班火车上的 Mac 笔记本电脑上进行更改,然后在我使用以下命令建立网络连接时将它们推送到 unfuddle:
git push unfuddle master
I use Capistrano for deployments and pull code from the unfuddle repository using the master branch.
我使用 Capistrano 进行部署,并使用 master 分支从 unfuddle 存储库中提取代码。
Lately I've noticed the following message when I run "git status" on my laptop:
最近,当我在笔记本电脑上运行“git status”时,我注意到以下消息:
# On branch master
# Your branch is ahead of 'origin/master' by 11 commits.
#
nothing to commit (working directory clean)
And I'm confused as to why. I thought my laptop was the origin... but don't know if either the fact that I originally pulled from Subversion or push to Unfuddle is what's causing the message to show up. How can I:
我很困惑为什么。我以为我的笔记本电脑是起源......但不知道是我最初从 Subversion 拉取还是推送到 Unfuddle 的事实导致消息出现。我怎样才能:
- Find out where Git thinks 'origin/master' is?
- If it's somewhere else, how do I turn my laptop into the 'origin/master'?
- Get this message to go away. It makes me think Git is unhappy about something.
- 找出 Git 认为 'origin/master' 在哪里?
- 如果它在其他地方,我如何将我的笔记本电脑变成“起源/主人”?
- 收到这条消息就走开。这让我觉得 Git 对某事不满意。
My mac is running Git version 1.6.0.1.
我的 Mac 运行的是 Git 版本 1.6.0.1。
When I run git remote show origin
as suggested by dbr, I get the following:
当我git remote show origin
按照 dbr 的建议运行时,我得到以下信息:
~/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
When I run git remote -v
as suggested by Aristotle Pagaltzis, I get the following:
当我git remote -v
按照 Aristotle Pagaltzis 的建议运行时,我得到以下信息:
~/Projects/GeekFor/geekfor 10:33 AM $ git remote -v
origin /Users/brian/Projects/GeekFor/gf/.git
unfuddle [email protected]:spilth/geekfor.git
Now, interestingly, I'm working on my project in the geekfor
directory but it says my origin is my local machine in the gf
directory. I believe gf
was the temporary directory I used when converting my project from Subversion to Git and probably where I pushed to unfuddle from. Then I believe I checked out a fresh copy from unfuddle to the geekfor
directory.
现在,有趣的是,我正在geekfor
目录中处理我的项目,但它说我的源是gf
目录中的本地机器。我相信这gf
是我在将我的项目从 Subversion 转换为 Git 时使用的临时目录,也可能是我从哪里推送的。然后我相信我从 unfuddle 到geekfor
目录检查了一个新副本。
So it looks like I should follow dbr's advice and do:
所以看起来我应该遵循 dbr 的建议并执行以下操作:
git remote rm origin
git remote add origin [email protected]:spilth/geekfor.git
采纳答案by dbr
1.
Find out where Git thinks 'origin/master' is usinggit-remote
1.
找出 Git 认为 'origin/master' 正在使用的地方git-remote
git remote show origin
..which will return something like..
..这将返回类似..
* remote origin
URL: [email protected]:~/something.git
Remote branch merged with 'git pull' while on branch master
master
Tracked remote branch
master
A remote is basically a link to a remote repository. When you do..
远程基本上是指向远程存储库的链接。当你做..
git remote add unfuddle [email protected]/myrepo.git
git push unfuddle
..git will push changes to that address you added. It's like a bookmark, for remote repositories.
..git 会将更改推送到您添加的地址。它就像一个书签,用于远程存储库。
When you run git status
, it checks if the remote is missing commits (compared to your local repository), and if so, by how many commits. If you push all your changes to "origin", both will be in sync, so you wont get that message.
当您运行时git status
,它会检查远程是否缺少提交(与您的本地存储库相比),如果是,则检查提交的数量。如果您将所有更改推送到“原点”,则两者都会同步,因此您不会收到该消息。
2.
If it's somewhere else, how do I turn my laptop into the 'origin/master'?
2.
如果它在其他地方,我如何将我的笔记本电脑变成“起源/主人”?
There is no point in doing this. Say "origin" is renamed to "laptop" - you never want to do git push laptop
from your laptop.
这样做没有意义。假设“origin”被重命名为“laptop”——你永远不想git push laptop
从你的笔记本电脑上做。
If you want to remove the origin remote, you do..
如果你想删除原点遥控器,你做..
git remote rm origin
This wont delete anything (in terms of file-content/revisions-history). This will stop the "your branch is ahead by.." message, as it will no longer compare your repository with the remote (because it's gone!)
这不会删除任何内容(在文件内容/修订历史方面)。这将停止“您的分支领先于..”消息,因为它将不再将您的存储库与远程存储库进行比较(因为它已经消失了!)
One thing to remember is that there is nothing special about origin
, it's just a default name git uses.
要记住的一件事是,没有什么特别之处origin
,它只是 git 使用的默认名称。
Git does use origin
by default when you do things like git push
or git pull
. So, if you have a remote you use a lot (Unfuddle, in your case), I would recommend adding unfuddle as "origin":
origin
当您执行git push
或 之类的操作时,Git默认会使用git pull
。因此,如果您有一个经常使用的遥控器(Unfuddle,就您而言),我建议将 unfuddle 添加为“来源”:
git remote rm origin
git remote add origin [email protected]:subdomain/abbreviation.git
or do the above in one command using set-url:
或使用 set-url 在一个命令中执行上述操作:
git remote set-url origin [email protected]:subdomain/abbreviation.git
Then you can simply do git push
or git pull
to update, instead of git push unfuddle master
然后你可以简单地做git push
或git pull
更新,而不是git push unfuddle master
回答by Earl Jenkins
I came to this question looking for an explanation about what the message "your branch is ahead by..." means, in the general scheme of git. There was no answer to that here, but since this question currently shows up at the top of Google when you search for the phrase "Your branch is ahead of 'origin/master'", and I have since figured out what the message really means, I thought I'd post the info here.
我来到这个问题是为了在 git 的一般方案中寻找关于“你的分支领先于......”消息的含义的解释。这里没有答案,但是由于当您搜索短语“您的分支领先于 'origin/master'”时,这个问题目前出现在 Google 的顶部,并且我已经弄清楚了该消息的真正含义,我以为我会在这里发布信息。
So, being a git newbie, I can see that the answer I needed was a distinctly newbie answer. Specifically, what the "your branch is ahead by..." phrase means is that there are files you've added and committed to your local repository, but have never pushed to the origin. The intent of this message is further obfuscated by the fact that "git diff", at least for me, showed no differences. It wasn't until I ran "git diff origin/master" that I was told that there were differences between my local repository, and the remote master.
所以,作为一个 git 新手,我可以看到我需要的答案是一个明显的新手答案。具体来说,“您的分支领先于...”这句话的意思是您已添加并提交到本地存储库的文件,但从未推送到源。至少对我而言,“git diff”没有显示出任何差异,这进一步混淆了这条消息的意图。直到我运行“git diff origin/master”,我才被告知我的本地存储库和远程 master 之间存在差异。
So, to be clear:
所以,要明确:
"your branch is ahead by..."=> You need to push to the remote master. Run "git diff origin/master"to see what the differences are between your local repository and the remote master repository.
“您的分支领先于...”=> 您需要推送到远程主服务器。运行“git diff origin/master”以查看本地存储库和远程主存储库之间的差异。
Hope this helps other newbies.
希望这可以帮助其他新手。
(Also, I recognize that there are configuration subtleties that may partially invalidate this solution, such as the fact that the master may not actually be "remote", and that "origin" is a reconfigurable name used by convention, etc. But newbies do not care about that sort of thing. We want simple, straightforward answers. We can read about the subtleties later, once we've solved the pressing problem.)
(此外,我认识到有些配置细节可能会使此解决方案部分无效,例如 master 可能实际上不是“remote”,并且“origin”是约定使用的可重新配置的名称等。但新手确实如此不关心那种事情。我们想要简单、直接的答案。一旦我们解决了紧迫的问题,我们可以稍后阅读其中的微妙之处。)
Earl
伯爵
回答by Mims H. Wright
I had a problem that was similar to this where my working directory was ahead of origin by X commits
but the git pull
was resulting in Everything up-to-date
. I did manage to fix it by following this advice. I'm posting this here in case it helps someone else with a similar problem.
我有一个与我的工作目录所在的类似的问题,ahead of origin by X commits
但git pull
导致Everything up-to-date
. 我确实按照这个建议设法修复了它。我在这里发布它以防它帮助其他有类似问题的人。
The basic fix is as follows:
基本修复如下:
$ git push {remote} {localbranch}:{remotebranch}
Where the words in brackets should be replaced by your remote name, your local branch name and your remote branch name. e.g.
括号中的单词应该替换为您的远程名称、您的本地分支名称和您的远程分支名称。例如
$ git push origin master:master
回答by chim
sometimes there's a difference between the local cached version of origin master (origin/master) and the true origin master.
有时,本地缓存版本的 origin master (origin/master) 和真正的 origin master 之间存在差异。
If you run git remote update
this will resynch origin master with origin/master
如果你运行git remote update
这将与 origin/master 重新同步 origin master
see the accepted answer to this question
看到这个问题的公认答案
Differences between git pull origin master & git pull origin/master
回答by Aristotle Pagaltzis
I thought my laptop was the origin…
我以为我的笔记本电脑是起源......
That's kind of nonsensical: origin
refers to the default remote repository – the one you usually fetch/pull other people's changes from.
这有点荒谬:origin
指的是默认的远程存储库——您通常从中获取/拉取其他人的更改的存储库。
How can I:
我怎样才能:
git remote -v
will show you whatorigin
is;origin/master
is your “bookmark” for the last known state of themaster
branch of theorigin
repository, and your ownmaster
is a tracking branchfororigin/master
. This is all as it should be.You don't. At least it makes no sense for a repository to be the default remote repository for itself.
It isn't. It's merely telling you that you have made so-and-so many commits locally which aren't in the remote repository (according to the last known state of that repository).
回答by Chris
[ Solution ]
[ 解决方案 ]
$ git push origin
^ this solved it for me. What it did, it synchronized my master (on laptop) with "origin" that's on the remote server.
^ 这为我解决了。它做了什么,它将我的主人(在笔记本电脑上)与远程服务器上的“来源”同步。
回答by Vino
It's waiting for you to "push". Try:
等你来“推”。尝试:
$ git push
$ git push
回答by Steve Hindmarch
I am struggling with this problem and none of the previous answers tackle the question as I see it. I have stripped the problem back down to its basics to see if I can make my problem clear.
我正在努力解决这个问题,以前的答案都没有解决我所看到的问题。我已经将问题归结为基本问题,看看我是否能把我的问题说清楚。
I create a new repository (rep1), put one file in it and commit it.
我创建了一个新的存储库 (rep1),将一个文件放入其中并提交。
mkdir rep1
cd rep1
git init
echo "Line1" > README
git add README
git commit -m "Commit 1"
I create a clone of rep1 and call it rep2. I look inside rep2 and see the file is correct.
我创建了一个 rep1 的克隆并将其称为 rep2。我查看 rep2 内部,发现文件是正确的。
cd ~
git clone ~/rep1 rep2
cat ~/rep2/README
In rep1 I make a single change to the file and commit it. Then in rep1 I create a remote to point to rep2 and push the changes.
在 rep1 中,我对文件进行了一次更改并提交。然后在 rep1 中,我创建了一个指向 rep2 的遥控器并推送更改。
cd ~/rep1
<change file and commit>
git remote add rep2 ~/rep2
git push rep2 master
Now when I go into rep2 and do a 'git status' I get told I am ahead of origin.
现在,当我进入 rep2 并执行“git status”时,我被告知我领先于原点。
# 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
#
README in rep2 is as it was originally, before the second commit. The only modifications I have done are to rep1 and all I wanted to do was push them out to rep2. What is it I am not grasping?
在第二次提交之前,rep2 中的 README 与原来的一样。我所做的唯一修改是对 rep1,我想做的就是将它们推出到 rep2。什么是我没有把握?
回答by looneydoodle
I had this problem recently and I figured it was because I had deleted some files that I did not need anymore. The problem is that git does not know that the files have been deleted and it sees that the server still has it. (server = origin)
我最近遇到了这个问题,我认为这是因为我删除了一些不再需要的文件。问题是 git 不知道文件已被删除,它看到服务器仍然有它。(服务器 = 原点)
So I ran
所以我跑了
git rm $(git ls-files --deleted)
And then ran a commit and push.
然后运行提交和推送。
That solved the issue.
那解决了这个问题。
回答by Nara Narasimhan
I am a git newbie as well. I had the same problem with 'your branch is ahead of origin/master by N commits' messages. Doing the suggested 'git diff origin/master' did show some diffs that I did not care to keep. So ...
我也是 git 新手。我对“你的分支在 N 个提交之前领先于 origin/master”消息有同样的问题。执行建议的 'git diff origin/master' 确实显示了一些我不想保留的差异。所以 ...
Since my git clone was for hosting, and I wanted an exact copy of the master repo, and did not care to keep any local changes, I decided to save off my entire repo, and create a new one:
由于我的 git clone 用于托管,并且我想要一个主存储库的精确副本,并且不关心保留任何本地更改,因此我决定保存我的整个存储库,并创建一个新的:
(on the hosting machine)
(在主机上)
mv myrepo myrepo
git clone USER@MASTER_HOST:/REPO_DIR myrepo
For expediency, I used to make changes to the clone on my hosting machine. No more. I will make those changes to the master, git commit there, and do a git pull. Hopefully, this should keep my git clone on the hosting machine in complete sync.
为方便起见,我曾经在我的主机上对克隆进行更改。不再。我将对 master 进行这些更改,在那里 git commit,然后执行 git pull。希望这能让我的 git clone 在主机上保持完全同步。
/Nara
/奈良