I do not work with git or the linux command line very often and it seems I mess it up a lot. I am a windows person and I use cygwin to emulate that environment.
Situation:
I have multiple repositories tracked, one I work on and a few others that are read only for me.
sometimes, when I do not pull from them for a long time (one was over 2k commits ahead of me) then sometimes git status
tells me, that the branches have diverged. I do not know how this happens, but I am then 2k commits behind and 1 ahead. Pulling then fails due to some files causing conflicts, that cannot be resolved.
This leaves me with the issue, that I now have a repository from which I cannot pull from anymore, because it has conflicts.
So my question is: how can I effectively pull from that repository again without running into conflicts? I do not care for any changes.
This is the solution I have to do each time it messes up:
git log --max-parents=0 head # copy ID on bottom
git reset --hard <ID-you-have-copied>
git clean -f -d
git pull
This is stupid and I like to have that in one command. I know of git aliases, but I have no idea, how to get the commit ID from command 1 into command 2. I am also fine with a shell script I can put somewhere, that does this for me, but it should be dynamic, without a hard coded ID.
It looks like I have to reset the branch just behind that faulty non-existent commit and then I can forward again
Is there something obvious that I am missing?
I am not sure what the git --max-parents=0 head
command is.
But I guess you could simply do this instead (origin & master may be replaced with desired remote and branch names):
git fetch origin master
git reset --hard FETCH_HEAD
git clean -f -d
This resets current branch pointer and working tree to the latest fetched commit (and removes untracked files in the same manner as your command).
NOTE: This removes all local changes in the repository, both committed and not committed changes. (committed changes can still be restored by referring to the git reflog
though)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With