使用Git命令的起源 - Use of origin in Git commands

- 此内容更新于:2016-02-02
主题:

我新的Git和有一个问题。这两个命令似乎是一个共同的配对:如果我的目标是重新设置本地克隆到一个干净的状态,比下面的有什么区别呢?或似乎抓取第一个不需要如果我们要指定远程直接。我误解什么?这是公约的目的没有基础或只是一个传统的习惯?为什么我问:我写脚本构建机器。我想取回所有在一个时间点,然后在当地的工作内容构建期间所以没有机会的变化进行构建。似乎从远程一次,我想取回不指定命令。然而,我很难找到在线的例子,所以我不确定是否有我失踪了。

原文:

I'm new to Git and have a question. These two commands seem to be a common pairing:

git fetch --all
git reset --hard origin/master

If my goal is to reset a local clone to a clean state, how is this different than either of the following?

git fetch --all
git reset --hard master
# no origin

Or

git reset --hard origin/master
# no fetch

It seems like fetching first isn't needed if we are going to specify the remote directly anyway. Am I misunderstanding something? Is this a convention with a purpose or just a tradition/habit without basis?

Why I'm asking:

I'm writing scripts for a build machine. I want to fetch everything at one point in time, and then work from the local content for the duration of the build so there isn't a chance of changes being made part-way through a build. It seems like I would want to fetch from the remote once and not specify origin in commands after that. However, I'm having a hard time finding examples of that online, so I wasn't sure if there was something I was missing.

楼主:以供将来参考,都是当地的分支机构,和他们是不同的。fetch实际上是需要更新本地起源/主分支。

(原文:For future reference, origin/master and master are both local branches, and they are different. The fetch is in fact needed to update the local origin/master branch.)

解决方案:
第一个远程分支机构的基本知识。存储库是这样的。和两个分支,只是标签,点提交。在这种情况下,你让两个提交主自从上次你获取。Git没有跟网络除非你告诉它。它跟踪上次看一个远程存储库“远程分支”。来源/主是一个“远程分支”。是你的主分支的本地副本在远程叫更新或者是最后一次运行(或者,获取)。这是用来扔掉你所有的地方工作。这将重置当前分支起源/主和工作目录,这是。并将丢失。这将重置您的当前分支和工作目录回到主人。如果你碰巧在掌握这将撤销你的未提交的工作。但如果你是在一个不同的分支,你吹你的分支。扔掉你的未提交的更改,更安全的做法是。是一个指向当前签出提交。它将工作无论如何你在分支。我有它的别名。我想取回所有在一个时间点,然后在当地的工作内容构建期间所以没有机会的变化进行构建。Git没有跟网络除非你告诉它。有两个命令将更新你的本地存储库,以及git拉(取回)。如果你不跑,你远程分支不会改变。如果你获取和结账的构建,你会没事的。如果你需要重置一个干净的状态可以运行git重置,艰难的头。您可能还想将扔掉任何无路径的运行或忽略文件构建构件。
原文:

First the basics of remote branches. Your repository looks something like this.

A - B - C [origin/master]
         \
          D - E [master]

master and origin/master are both branches, which are just labels that point at a commit. In this case, you've made two commits to master since the last time you fetched.

Git doesn't talk to the network unless you tell it to. It keeps track of the last time it looked at a remote repository with a "remote branch". origin/master is a "remote branch". It is your local copy of the master branch on the remote named origin updated the last time git fetch origin or git fetch --all was run (or git pull, which does a fetch).


git reset --hard origin/master

This is used to throw out all your local work. This will reset the current branch and working directory to where origin/master is, which is C. D and E will be lost.

A - B - C [origin/master]
         \
          D - E [master]

git reset --hard origin/master

A - B - C [origin/master]
          [master]

git reset --hard master

This will reset your current branch and working directory back to master. If you happen to be on master this will undo your uncommitted work. But if you're on a different branch, you just blew away your branch.

To throw out your uncommitted changes, the safer thing to do is git reset --hard HEAD. HEAD is a pointer to the currently checked out commit. It will work no matter what branch you're on. I have it aliased to clear.


I want to fetch everything at one point in time, and then work from the local content for the duration of the build so there isn't a chance of changes being made part-way through a build.

Git does not talk to the network unless you tell it to. There's two commands which will update your local repository, git fetch and git pull (which does a fetch). If you don't run those, your origin/ remote branches will not change.

If you fetch and checkout at the start of your build, you'll be fine. If you need to reset to a clean state you can run git reset --hard HEAD. You might also want to run git clean -dxf which will throw out any untracked or ignored files like build artifacts.

楼主:所以我误解了,这句话为我清除了:“是一个“远程分支”。这是你的远程分支的本地副本”。我在想这是远程的。谢谢!

(原文:So I did misunderstand about using origin/, this sentence cleared it up for me: "origin/master is a 'remote branch'. It is your local copy of the master branch on the remote". I was thinking it was remote. Thanks!)