简体   繁体   English

什么是从远程仓库获取所有更改的git命令?

[英]What's the git command to pull in all changes from a remote repo?

I have a repo that's forked from a remote repository. 我有一个从远程存储库分叉的repo。 I made some changes, the remote repository updated and now I want to pull in all changes from the remote repository, and not care about anything in my local repository. 我做了一些更改,远程存储库已更新,现在我想从远程存储库中提取所有更改,而不关心本地存储库中的任何内容。 Previously I've been deleting my local repository, then doing a simple fork and clone. 以前我一直在删除我的本地存储库,然后做一个简单的fork和clone。 There's got to be a better way to do this. 必须有一个更好的方法来做到这一点。 What's the magic command? 什么是魔术命令?

If I understand you correctly, you want to throw away commits on your local branch (if you've made any). 如果我理解正确,你想丢弃你当地分支机构的提交(如果你已经做了)。 If that's the case, then you want: 如果是这种情况,那么你想:

# fetch updated branch(es) from origin
git fetch origin

# reset, including your work tree, to origin's master branch
# make sure you have your master branch checked out first!
# and also that you don't mind throwing away local commits or
# uncommitted modifications
git reset --hard origin/master

Some assumptions: master is the old branch, where you commit some changes. 一些假设: master是旧分支,您可以在其中进行一些更改。 Now other is the fresh checkout from the remote origin . 现在, other是来自远程origin的新结账。

git fetch origin
git checkout -b other origin/master

With

git diff other..master

you can compare the two branches. 你可以比较两个分支。 And at last with 最后还有

git checkout other
git merge master

you merge them. 你合并他们。 Another useful tool here is cherry-pick , with that you can merge only some interesting commits into a branch 另一个有用的工具是cherry-pick ,你可以将一些有趣的提交合并到一个分支中

git cherry-pick <commit>

A simple way would be to branch, like this: 一个简单的方法是分支,像这样:

git commit -m "All my latest stuff I don't care about"
git branch newstuff refs/remotes/origin/master 
git pull

And now you've got all of your new stuff. 现在你已经拥有了所有新东西。 Of course this is assuming you want to keep the old stuff. 当然这是假设你想要保留旧的东西。

(Considering your branch is master and the remote is named origin ) (考虑到你的分支是master ,遥控器被命名为origin

First update your origin with: 首先更新您的origin

git fetch origin

Now, if you have not committed since your last update you can simply do this: 现在,如果您自上次更新后未提交,则可以执行以下操作:

git rebase master origin/master

If you have some commits done, this fast-forward rebase won't work. 如果你有一些提交,这个快速的 rebase将无法正常工作。 In that case you can do: 在这种情况下,你可以这样做:

git branch -d master (to remove your local master branch)

git checkout -b master origin/master


Diffing before merging: 合并之前的差异:

If you want to see what changed before doing the merge do: 如果你想在进行merge之前看到改变了什么:

git fetch origin (always to bring the changes from the remote)

git diff master origin/master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM