同步本地git仓库与远程一次性丢弃本地更改/提交 [英] Sync local git repo with remote in one shot discarding local changes/commits

查看:164
本文介绍了同步本地git仓库与远程一次性丢弃本地更改/提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是git专家,所以这可能看起来像一个愚蠢的问题。



我有本地和远程存储库,我想同步我的本地远程存储库。我有许多本地更改,存储了更改,还有一些提交还没有推送到远程。



一种方法可以删除所有本地更改(使用 git clean ),从远程恢复提交并比 fetch / pull 。但我认为必须有一个能够一次完成所有这些工作的单一命令。我尝试使用 git reset --hard HEAD ,然后 git pull 但是这给了我:

 #你的分支和'origin / master'分别有
#和1个和9个不同的提交。

看着这个问题,但没有帮助。 解决方案

正如所评论的, git reset --hard origin / master 会将您的 master 重置为上游。 / p>

但是:您当前的主人(使用本地提交)将会丢失(至少不再可见)。

所以 one 步骤不是最好的方法。



我会推荐如下内容:

  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

通过这种方式,您可以在更新后的主设备上重新绑定您的本地提交

 <$ c $ x> x  -  x  -  x  -  x  -  x(master,origin / master)x  -  x  -  x  - -x  -  x  -  y' -  y'(tmp,master)
\ =>
y - y(tmp)(rebase +合并后)


I'm a not a git expert so this might look like a silly question.

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. 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:

# 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.

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

x--x--x--x--x (master, origin/master)       x--x--x--x--x--y'--y' (tmp, master)
       \                                 =>
        y--y  (tmp)                           (after rebase+merge)

这篇关于同步本地git仓库与远程一次性丢弃本地更改/提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆