Clean Local Working Branch
Sometimes if the local working branch messes up but you want to sync up with your remote branch, for example origin master, first remove all untracked files:
1 | # -d: remove untracked dirs |
you can have a dry-run first to see what files will be removed
1 | # -n: dry run |
Check the commit logs in current branch:
1 | # verbose |
Then properly select one good commit that also appears in remote master branch, run
1 | git reset --hard <good commit hash> |
All the commits later than the good commit hash will be removed, then you can just run the command below to sync up remote master branch.
1 | git pull origin master |
Note that it’s safe to do git reset here because only I use this branch and I want to clean
Sometimes I see people use:
1 | # reset to current commit, discard all uncommitted changes |
HEAD
points to your current commit, check by:
1 | # get HEAD short hash |
so all that git reset --hard HEAD
will do is to throw away any uncommitted changes you have and point to your latest commit.