用于检出任何分支并覆盖本地更改的 Git 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18487089/
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
Git command to checkout any branch and overwrite local changes
提问by szeryf
Is there a Git command (or a short sequence of commands) that will safely and surely do the following?
是否有一个 Git 命令(或一个简短的命令序列)可以安全可靠地执行以下操作?
- Get rid of any local changes.
- Fetch the given branch from origin if necessary
- Checkout the given branch?
- 摆脱任何本地更改。
- 如有必要,从原点获取给定的分支
- 结帐给定的分支?
Currently I'm stuck with:
目前我坚持:
git fetch -p
git stash
git stash drop
git checkout $branch
git pull
but it's bothering me because I'm asked for password two times (by fetch
and pull
). Generally I would be happy with any solution as long as the password is needed only once.
但它困扰着我,因为我被要求输入密码两次(通过fetch
和pull
)。一般来说,只要密码只需要一次,我就会对任何解决方案感到满意。
A couple of notes:
一些注意事项:
- It's a part of homebrewed deployment script for an application (the code is hosted on GitHub).
- There should be no difference if the branch was already fetched from origin or not (i.e. the first deployment of a new branch shouldn't ideally require any additional steps).
- The script is located on a remote machine that can be accessed by several people, hence no credentials are stored and user/password must be entered (but only once if possible).
- I don't care about any local changes; I always want a pristine copy of the given branch (the further part of deployment script produces local changes).
- I can't clone or export a fresh repository each time; it takes too much time.
- 它是应用程序的自制部署脚本的一部分(代码托管在 GitHub 上)。
- 如果分支是否已经从源获取,应该没有区别(即,理想情况下,新分支的第一次部署不应该需要任何额外的步骤)。
- 该脚本位于可由多人访问的远程机器上,因此不存储任何凭据且必须输入用户/密码(但如果可能,只输入一次)。
- 我不在乎任何本地变化;我总是想要给定分支的原始副本(部署脚本的另一部分产生本地更改)。
- 我不能每次都克隆或导出一个新的存储库;这需要太多时间。
回答by VonC
You could follow a solution similar to "How do I force “git pull” to overwrite local files?":
您可以遵循类似于“如何强制“git pull”覆盖本地文件?”的解决方案:
git fetch --all
git reset --hard origin/abranch
git checkout $branch
That would involve only one fetch.
那将只涉及一次提取。
With Git 2.23+, git checkout
is replaced here with git switch
(presented here)(still experimental).
在 Git 2.23+ 中,git checkout
此处替换为git switch
(此处提供)(仍处于实验阶段)。
git switch -f $branch
(with -f
being an alias for --discard-changes
, as noted in Jan's answer)
(-f
作为 的别名--discard-changes
,如Jan的回答中所述)
Proceed even if the index or the working tree differs from HEAD.
Both the index and working tree are restored to match the switching target.
即使索引或工作树与 HEAD 不同,也要继续。
恢复索引和工作树以匹配切换目标。
回答by Marcin ?o?
Couple of points:
几点:
- I believe
git stash
+git stash drop
could be replaced withgit reset --hard
... or, even shorter, add
-f
tocheckout
command:git checkout -f -b $branch
That will discard any local changes, just as if
git reset --hard
was used prior to checkout.
- 我相信
git stash
+git stash drop
可以替换为git reset --hard
...或者,甚至更短,添加
-f
到checkout
命令:git checkout -f -b $branch
这将丢弃任何本地更改,就像
git reset --hard
在结帐之前使用一样。
As for the main question:
instead of pulling in the last step, you could just merge the appropriate branch from the remote into your local branch: git merge $branch origin/$branch
, I believe it does not hit the remote. If that is the case, it removes the need for credensials and hence, addresses your biggest concern.
至于主要问题:您可以将远程分支中的适当分支合并到本地分支中git merge $branch origin/$branch
,而不是进入最后一步:,我相信它不会击中远程分支。如果是这种情况,则不需要凭据,因此可以解决您最关心的问题。
回答by Aaron Hull
git reset
and git clean
can be overkill in some situations (and be a huge waste of time).
git reset
并且git clean
在某些情况下可能会矫枉过正(并且会浪费大量时间)。
If you simply have a message like "The following untracked files would be overwritten..." and you want the remote/origin/upstream to overwrite those conflicting untracked files, then git checkout -f <branch>
is the best option.
如果您只是收到“以下未跟踪文件将被覆盖...”之类的消息,并且您希望远程/来源/上游覆盖那些冲突的未跟踪文件,那么这git checkout -f <branch>
是最佳选择。
If you're like me, your other option was to clean and perform a --hard reset
then recompile your project.
如果你像我一样,你的另一个选择是清理并执行--hard reset
然后重新编译你的项目。
回答by Jan
The new git-switch
command(starting in GIT 2.23) also has a flag --discard-changes
which should help you. git pull
might be necessary afterwards.
新git-switch
命令(从 GIT 2.23 开始)还有一个标志--discard-changes
可以帮助你。git pull
之后可能需要。
Warning: it's still considered to be experimental.
警告:它仍然被认为是实验性的。