Git: how to update the working directory like hg up -C in Mercurial

混江龙づ霸主 提交于 2019-12-24 01:23:52

问题


I previously worked with the mercurial, but now with git. there were some problems, because git still a little different.

Could you suggest how you can update the git repository to the last commit, discarding all changes and conflicts, as it does for example hg up -C in Mercurial

I was advised to command git checkout -f, it displays this message:

Your branch and 'origin/master' have diverged, and have 24 and 5 different commit(s) each, respectively.

After applying this command, the files in the working directory are not updated.


回答1:


First, the error message you are getting is telling you that the local and remote versions of your branch have diverged. In order to get the changes from the remote branch you will either have to merge or rebase. Assuming you want to use a merge strategy, you can try this:

git fetch                             # get the latest changes from the remote
git checkout theBranch                # checkout your local branch
git merge -X theirs origin/theBranch  # merge with remote version of the branch

The -X theirs option in the git merge means that you will unconditionally keep the version of the files coming from the remote, overwriting whatever version you might have locally.

As this SO article discusses, if the merge results in a local file being deleted, then you may have to manually remove it using:

git rm /path/to/deleted/file

Then you can make your merge commit, and continue working.




回答2:


The message you got is simply telling you that there is a different between the branch you've currently checked out and the local copy of its counterpart in the remote repository called origin. Then it proceeds to tell you that there are a number of commits local that are not remote and vice versa.

Simply pulling remote changes in and pushing remote changes out will remove this message. Git is very informative about a lot. This is nothing to worry about.

If git status does not show anything other right now you actually have discarded all changes that were not committed. One way is checking out a file, as you presumably did. This enable you to reset changes to a single or a couple of files. Another way is resetting your branch to head (simplified: the newest commit) using git reset HEAD --hard This obviously removes all changes in all files that were not committed.

Without the hard flag reset resets staged changes to unstaged changes. With the hard flag all changes are unchanged and undone.



来源:https://stackoverflow.com/questions/33255900/git-how-to-update-the-working-directory-like-hg-up-c-in-mercurial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!