执行 git pull 时如何强制更新?

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

How to force update when doing git pull?

git

提问by Blankman

I'm doing git pull of an open source lib, I don't care what my local copy is I just want to update it with the origin's version.

我正在做一个开源库的 git pull,我不在乎我的本地副本是什么,我只想用原始版本更新它。

i.e. it can blow away my local changes.

即它可以吹走我的本地更改。

回答by Luca Filipozzi

This should do the trick:

这应该可以解决问题:

git reset --hard HEAD
git pull

回答by jwg

git fetch
git checkout origin/name_of_branch  # correct state but in 'head without a name'
git checkout -B name_of_branch      # overwrite current tree onto name_of_branch

This checks out the remote tracking branch into a head without a name, then you can have a look and check you're happy. Whenever you want to (even after changes or commits) the second git checkout labels your current tree with 'name_of_branch', even if it has to delete the old name_of_branch to do so.

这会将远程跟踪分支检查为没有名称的头部,然后您可以查看并检查您是否满意。无论何时(即使在更改或提交之后),第二个 git checkout 都会用“name_of_branch”标记当前树,即使它必须删除旧的 name_of_branch 才能这样做。



Edit: 'What a crazy command line syntax'

编辑:'多么疯狂的命令行语法'

The syntax of git commands seems unintuitive. In fact it's the Git data model that is unintuitive. This isn't bad design, but because it works with files, branches, commits in ways that are much more flexible and powerful than other version control systems. Once you grok how these things work in Git, the command line will make a lot of sense and you will be able to accurately guess how to do complicated things.

git 命令的语法似乎不直观。事实上,不直观的是 Git 数据模型。这不是一个糟糕的设计,但因为它以比其他版本控制系统更灵活和强大的方式处理文件、分支和提交。一旦你理解了这些东西在 Git 中是如何工作的,命令行就会变得很有意义,你将能够准确地猜测如何做复杂的事情。

  • git fetchThis fetchesall the updates that have happened on the originserver since the last time. Git separates fetching from the server from updating, merging, etc. any of your branches. All the branches called origin/XXXare yourmost recent versions of what's on the server. They are not remote branchesbut remote tracking branches, local branches which track remote branches. When you do git fetch, you update them, without touching any of your own branches. When you do git merge, git rebase, and so on, you use the fetched version, withoutgrabbing a more recent copy from the server. This means you have control over when network requests are made (if you aren't always connected to the server), and you can 'fetch' a snapshot of the server, then merge at your leisure. If in doubt, do git fetchfirst.
  • git checkout origin/name_of_branchgit checkoutdoes two things. It updates your files to that branch, and it sets your HEADto that branch. The files things is what you'd expect. The HEADthings means that when you do commits, they will added to the end of the branch that HEADpoints to. IOW, checkoutdoes both output- write the right versions of the files, and prepares for input- gets ready to store the commits you are going to do in the right place. If you checkout a branch called foo, your tree changes to the state saved in foo, andthe next commits you do will be added to foo. In the case of origin\xyz, you can't write your changes to an originbranch - these track what is on the originserver and can't be committed to directly. So the checkout updates the files, and sets HEADto nothing - an unnamed branch. This stops you accidentally committing your newly checked out stuff back onto the previous branch you were using, and in fact you will be heavily discouraged by git from committing at all until you have a destination branch.
  • git checkout -B name_of_branchAs usual, when git does two different things with one command, both of them can be configured separately. So the second part of what checkoutdoes, setting HEADto the branch you want to commit to, can be to a new branch, instead of the branch you're checking out, if you use the -Boption. So git checkout -B new_stuff old_stuffwill set all your files to the state in old_stuff, but get you ready to write your commits to the new branch new_stuff. This is a common task, and saves you checking out a branch then forking it, in two steps. Now, almost all arguments to git commands can be omitted, and git will do the most obvious thing. In this case, that is making a new branch based on the one you are on, rather than one you want to checkout. So git takes the unnamed branch you are on, and makes a new branch called name_of_branch, which you can start committing to. Note that an uppper case letter "B" kind of means 'force'. If you used "-b" git would refuse to overwrite an already existing branch. With "-B" it will go ahead and do it without warning or confirming.
  • git fetch这将获取origin自上次以来服务器上发生的所有更新。Git 将从服务器获取与更新、合并等任何分支分开。所有叫分支origin/XXX的服务器上有什么最新版本。它们不是远程分支,而是远程跟踪分支,跟踪远程分支的本地分支。当您这样做时git fetch,您会更新它们,而不会触及您自己的任何分支。当您执行git mergegit rebase等操作时,您将使用获取的版本,而没有从服务器获取更新的副本。这意味着您可以控制何时发出网络请求(如果您不总是连接到服务器),并且您可以“获取”服务器的快照,然后在您空闲时进行合并。如果有疑问,git fetch请先做。
  • git checkout origin/name_of_branchgit checkout做两件事。它会将您的文件更新到该分支,并将您的文件设置HEAD为该分支。文件内容正是您所期望的。这HEAD意味着当您提交时,它们将添加到HEAD指向的分支的末尾。IOW,checkout两个输出- 编写正确版本的文件,并准备输入- 准备好将您要做的提交存储在正确的位置。如果您检出一个名为 的分支foo,您的树将更改为保存在 中的状态foo并且您所做的下一次提交将被添加到 中foo。在 的情况下origin\xyz,您不能将更改写入origin分支 - 这些会跟踪分支上的内容origin服务器,不能直接提交。所以结帐更新文件,并设置HEAD为空 - 一个未命名的分支。这可以防止您不小心将新签出的内容提交回您正在使用的前一个分支,实际上,在您拥有目标分支之前,git 会严重劝阻您根本不提交。
  • git checkout -B name_of_branch像往常一样,当 git 用一个命令做两个不同的事情时,它们都可以单独配置。因此,如果您使用该选项,那么checkout设置HEAD到您想要提交的分支的第二部分可以是一个新的分支,而不是您正在检出的分支-B。Sogit checkout -B new_stuff old_stuff会将您的所有文件设置为 中的状态old_stuff,但让您准备好将您的提交写入新分支new_stuff。这是一项常见任务,无需您分两步检查分支然后分叉它。现在,几乎所有 git 命令的参数都可以省略,而 git 会做最明显的事情。在这种情况下,这是根据您所在的分支创建一个新分支,而不是您想要结帐的。因此 git 使用您所在的未命名分支,并创建一个名为 的新分支name_of_branch,您可以开始提交到该分支。请注意,大写字母“B”的意思是“力”。如果您使用“-b”,git 将拒绝覆盖已经存在的分支。使用“-B”,它将在没有警告或确认的情况下继续执行。

回答by antak

The go-to, knee-jerk, solution is: git reset --hard origin/master

去到,下意识的,解决方法是:git reset --hard origin/master

It's the almighty solution for experts and beginners alike that swiftly gets the job done. Albeit while blowing away all uncommitted changes without warning.

对于专家和初学者来说,这是一个万能的解决方案,可以快速完成工作。 尽管在没有警告的情况下吹走所有未提交的更改。

The safercommand is slightly more of a pain to type: git checkout -B master origin/master

安全指令稍微痛苦的是类型:git checkout -B master origin/master

Enter aliases:

输入别名:

git config --global alias.become '!git checkout -B "$(git symbolic-ref --short HEAD)"'

Henceforth, one can type: git become origin/master

从今以后,可以输入: git become origin/master