“git fetch && git checkout”与“git checkout”之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50844087/
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-19 13:08:15  来源:igfitidea点击:

What's the difference among "git fetch && git checkout" versus "git checkout" only?

gitgit-checkoutgit-fetch

提问by mario ruiz

Should do we always do as:

我们应该总是这样做:

git fetch && git checkout

Or only,

或者只是,

git checkout

?

?

For example when doing a checkout from a branch in bitbucket they provide the command as:

例如,当从 bitbucket 中的分支结帐时,他们提供的命令为:

enter image description here

在此处输入图片说明

git fetch && git checkout develop

But why is this necessary if

但是为什么有必要,如果

git checkout

结帐

will do the same, isn't it?

会做同样的,不是吗?

采纳答案by isaac weathers

To chime in here since I have to use Bitbucket daily for multiple projects and multiple branches I will give you my recommendation.

由于我必须每天将 Bitbucket 用于多个项目和多个分支,所以在这里补充一下,我会给你我的建议。

If you checkout from Bitbucket, i.e. create a branch, then you should be ok using the commands that they have provided as you pasted in your example. However, since it is likely that after the initial checkout you will be switching branches, creating branches and your local will get out of sync I recommend the following using your terminal. :

如果您从 Bitbucket 结帐,即创建一个分支,那么您应该可以使用他们在示例中粘贴时提供的命令。但是,由于在初始结帐后您可能会切换分支、创建分支并且您的本地分支将不同步,因此我建议使用您的终端执行以下操作。:

  1. git checkout developor whatever branch you need
  2. git fetch && git pulli.e. fetch all branches and latest changes as well as pull in all changes from the branch you are on.
  1. git checkout develop或您需要的任何分支
  2. git fetch && git pull即获取所有分支和最新更改以及从您所在的分支中提取所有更改。

Yes this does seem like duplicate work but working with Bitbucket I will say that this is the safest and sanest way to ensure that you have the latest from the branch that you are working on.

是的,这确实看起来像是重复的工作,但与 Bitbucket 一起工作,我会说这是确保您从正在处理的分支中获得最新信息的最安全和最明智的方法。

That being said, you should always create branches and never push directly to your developor masterbranches.

话虽如此,您应该始终创建分支,而不要直接推送到您的developmaster分支。

So let's say that you are on developbranch and you have done the above by checking out the branch and have fetched and pulled the latest you would then create a branch off of that main branch using standard git checkout -b my-feature-branch

因此,假设您在develop分支上,并且通过检查分支完成了上述操作,并获取并提取了最新的分支,然后您将使用标准从该主分支创建一个分支git checkout -b my-feature-branch

Example of what we do at my shop:

我们在我的店里做的例子:

  1. git checkout develop
  2. git fetch && git pull
  3. git checkout -b feature/JIRA_ISSUE_NUMBER-update-layout-for-this-page
  1. git checkout develop
  2. git fetch && git pull
  3. git checkout -b feature/JIRA_ISSUE_NUMBER-update-layout-for-this-page

Now you have checked out the develop branch, pulled down all the latest changes and remote branches and created a feature branch from that develop branch.

现在您已经检查了开发分支,拉下所有最新更改和远程分支,并从该开发分支创建了一个功能分支。

Hope this helps.

希望这可以帮助。

回答by javierojeda

git fetchpulls references to the remote branches that are being created on (in this case) BItbucket.

git fetch提取对正在(在本例中)BItbucket 上创建的远程分支的引用。

git checkoutmoves your current directory into a specific branch or a specific commit (depending the parameter).

git checkout将当前目录移动到特定分支或特定提交(取决于参数)。

What's going on here? When you create a branch on your bitbucket, they provide you with that command because: 1- The branch was created on the REMOTE repository. Your local copy of the repository doesn't have that new branch yet. That's what the git fetchcommand is used for. 2- Bitbucket assumes that, since you just created that new branch, you will work on it immediately and you need to tell that to your local repository. That's what the git checkoutcommand is used for

这里发生了什么?当您在 bitbucket 上创建分支时,他们会为您提供该命令,因为: 1- 该分支是在 REMOTE 存储库上创建的。您的存储库的本地副本还没有那个新分支。这就是git fetch命令的用途。2- Bitbucket 假设,由于您刚刚创建了该新分支,您将立即对其进行处理,并且您需要将其告知您的本地存储库。这就是git checkout命令的用途

That is not the only way to achieve it. You could, for example, aviod that git fetchcommand by using:

这不是实现它的唯一方法。例如,您可以git fetch使用以下命令来避免该命令:

git checkout -b {new_branch_name} && git pull origin {new_branch_name}

git checkout -b {new_branch_name} && git pull origin {new_branch_name}

That's not the most practical way, but probably will give you some better idea of how those commands work.

这不是最实用的方法,但可能会让您更好地了解这些命令的工作原理。

回答by sumit singh

To be specific to your question " when doing a checkout from a branch in bitbucket they provide the command as:git fetch && git checkout develop". This is because develop branch being created at cloud or remote , will not be available at you local machine until and unless you fetch the updates from remote.

具体到您的问题“当从 bitbucket 中的分支进行结账时,他们提供的命令为:git fetch && git checkout develop”。这是因为在云或远程创建的开发分支在本地机器上不可用,除非您从远程获取更新。

Thus, to switch or checkout to develop branch , you first need to fetch all the remote updates to be aware of existence of develop branch. Once you do a fetch, your local reppo will be aware of the new branch being created at remote; and when you do a checkout to develop, it will setup a new local branch to track its remote couterpart.

因此,要切换或结帐到 develop 分支,您首先需要获取所有远程更新以了解 develop 分支的存在。一旦你做了一个 fetch,你的本地仓库就会知道在远程创建的新分支;当您结帐进行开发时,它会设置一个新的本地分支来跟踪其远程节点。

回答by hspandher

git fetchupdates the origin/<branch_name>with the remote. git checkoutcan be used to switch branches. Both have no co-relation whatsoever, unless either you're trying to switch to a branch that is newly created in the remote and it's local version doesn't exist on your system.

git fetchorigin/<branch_name>用遥控器更新。git checkout可用于切换分支。两者都没有任何关联,除非您尝试切换到在远程新创建的分支并且它的本地版本在您的系统上不存在。

When you do git pull, however, (or git fetchand git rebase) and then git checkout -b <another_branch>to create a new one, you're making sure that your new branch is forked with the latest version of stable code.

git pull但是,当您执行, (或git fetchgit rebase)然后git checkout -b <another_branch>创建一个新分支时,您要确保您的新分支使用最新版本的稳定代码分叉。

回答by Zac R.

git fetchwill pull down all changes from your remote location

git fetch将从您的远程位置拉下所有更改

git checkoutwill switch you to a different branch (or restore your files to a previous state, depending how you use it)

git checkout会将您切换到不同的分支(或将您的文件恢复到以前的状态,具体取决于您的使用方式)

Use fetch and checkout to switch branches and pull all updated files. Use only checkout to switch branches, but continue working on your local version.

使用 fetch 和 checkout 切换分支并拉取所有更新的文件。只使用 checkout 来切换分支,但继续在你的本地版本上工作。

回答by taltamir

There is a problem with the question. In that it is missing a step. You actually need 3 steps to properly switch your working branch. If you just do the 2 steps described in your OP then you are in for some pain.

问题有问题。因为它缺少一个步骤。您实际上需要 3 个步骤来正确切换您的工作分支。如果您只执行 OP 中描述的 2 个步骤,那么您会遇到一些痛苦。

For a simple explanation with minimal jargon, if you want to locally work on a branch called devAthe 3 steps are:

用最少的行话做一个简单的解释,如果你想在一个名为devA3 个步骤的分支上本地工作:

Step 1: git fetch --allalthough you could technically fetch just this one branch. It is a good idea to get into a habit of always doing fetch --all This command makes git find out the state of the online repository actually is. It generally should be done before any operation and many people run scripts that aggressively do this automatically every minute.

第 1 步:git fetch --all虽然从技术上讲,您可以只获取这一分支。养成始终执行 fetch --all 的习惯是个好主意,此命令使 git 找出在线存储库的实际状态。它通常应该在任何操作之前完成,许多人运行的脚本每分钟都会自动执行此操作。

For example, if you just try to go to step 2 without doing fetch all first, then it could very well return an error that said branch does not actually exist. Or switch to an outdated version of it and falsely inform you that you are up to date with the online repository when in fact you are not. Thus tricking you into thinking you do not need to do step 3

例如,如果您只是尝试转到第 2 步而不先执行 fetch all,那么它很可能会返回一个错误,指出该分支实际上并不存在。或者切换到它的过时版本并错误地通知您您是最新的在线存储库,而实际上您不是。从而诱使您认为您不需要执行第 3 步

Step 2: git checkout devAthis just switches your git to work on that branch. simple.

第 2 步:git checkout devA这只是将您的 git 切换到在该分支上工作。简单的。

Step 3: git pullthis actually updates your currently worked on branch (see step 2) to match the online repository. If you do not do this then next time you try to commit changes you will accidentally break stuff. Although if it is the very first time you ever checkout a branch on the current machine you do not need to use this command.

第 3 步:git pull这实际上更新了您当前工作的分支(参见第 2 步)以匹配在线存储库。如果您不这样做,那么下次您尝试提交更改时,您将不小心破坏内容。尽管如果这是您第一次在当前机器上签出分支,则不需要使用此命令。

With all that in mind, going back to the original question

考虑到所有这些,回到最初的问题

git fetch && git checkout= first find out what the state of the repository is. then switch to a branch.

git fetch && git checkout= 首先找出存储库的状态。然后切换到一个分支。

git checkout= without bothering to find out what the state of a repository is, try to switch to a branch. This could tell you the branch does not exist. Or it could switch to an outdated version of the branch while falsely telling you that it is up to date with the repository.

git checkout= 无需费心找出存储库的状态,尝试切换到分支。这可以告诉您该分支不存在。或者它可能会切换到分支的过时版本,同时错误地告诉您它与存储库是最新的。