Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reset git repository once and for all into a clean state

Tags:

git

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?

like image 208
AyrA Avatar asked Oct 19 '22 02:10

AyrA


1 Answers

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)

like image 171
Alderath Avatar answered Oct 22 '22 00:10

Alderath