[英]Sync local git repo with remote in one shot discarding local changes/commits
I'm a not a git expert so this might look like a silly question.我不是 git 专家,所以这可能看起来像一个愚蠢的问题。
I have local and remote repositories, I want to sync my local with the remote repository.我有本地和远程存储库,我想将我的本地存储库与远程存储库同步。 I have many local changes, stashed changes, and few commits which are not yet pushed to remote.
我有许多本地更改、隐藏的更改以及尚未推送到远程的少量提交。
One way can be to remove all local changes(using git clean
), revert commits and than fetch/pull
from remote.一种方法是删除所有本地更改(使用
git clean
),恢复提交,而不是从远程fetch/pull
。 But I think there must be some single command which can do all this in one go.但我认为必须有一些单一的命令可以一次性完成所有这些。 I tried using
git reset --hard HEAD
and then git pull
but that gave me:我尝试使用
git reset --hard HEAD
然后git pull
但这给了我:
# Your branch and 'origin/master' have diverged,
# and have 1 and 9 different commits each, respectively.
Was looking at this question as well, but didn't help.也在看这个问题,但没有帮助。
As commented, a git reset --hard origin/master
would reset your master
to upstream.如评论所述,
git reset --hard origin/master
会将您的master
服务器重置为上游。
But: your current master (with its local commit) would be "lost" (at least, no longer visible).但是:您当前的主人(及其本地提交)将“丢失”(至少,不再可见)。
So one step isn't the best approach.所以一步不是最好的方法。
I would recommend something like:我会推荐类似的东西:
git checkout master
git fetch origin
git branch tmp
git reset --hard origin/master
git checkout tmp
git rebase master
git checkout master
git merge tmp
git branch -d tmp
That way, you rebase your local commit(s) on top of the updated master这样,您可以在更新的 master 之上重新设置本地提交
x--x--x--x--x (master, origin/master) x--x--x--x--x--y'--y' (tmp, master)
\ =>
y--y (tmp) (after rebase+merge)
The permanent fix if you always want to create a new branch on the remote to mirror and track your local branch(or vice-versa) is:如果您总是想在遥控器上创建一个新分支来镜像和跟踪您的本地分支(反之亦然),那么永久修复是:
git config --global push.default current
I always configure my local git with this command after I do git clone.执行 git clone 后,我总是使用此命令配置本地 git。 Although it can be applied anytime when the local-remote branch "Git fatal: The current branch has no upstream branch" error occurs.
虽然它可以在本地远程分支“Git 致命:当前分支没有上游分支”错误发生时随时应用。
Hope this helps.希望这可以帮助。 Much peace.
多平安。 :)
:)
This command will discard all local commits on a branch and reset it to mirror what is currently on the remote:此命令将丢弃分支上的所有本地提交并将其重置为镜像远程当前的内容:
git pull
git reset --hard origin/your-branch
The pull command is to make sure what you have is the most up to date of the branch pull 命令是为了确保你拥有的是最新的分支
git fetch -[options]
Checkout:查看:
git fetch --help
for the functionalities you can use对于您可以使用的功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.