The git remaster command

OK, OK. This command doesn't exist. It's one of the aliases that I'm using.
TL;DR
Execute this command once only.
git config --global alias.remaster '!git fetch && git clean -d -f && git reset --hard origin/master'
When you totally fucked up your local repository and you want an atomic cleaning, use this one.
git remaster
Your current branch will be in the same state as the remote master (the one which is on Gitlab, Github...)
The long version
I'm writing this article because I saw this tweet from Emma Bostian. I was doing the same until I've found a solution. Btw, you should follow her.
https://twitter.com/EmmaBostian/status/1232412585397571584
In this part I will explain in detail the meaning of this command.
git config --global alias.remaster '!git fetch && git clean -d -f && git reset --hard origin/master'
I will explain first how to configure alias (this part
git config --global alias.remaster
The alias
Alias is just "a shorter name for something that is normally long to write". For example in maths you're using the
=
is equal to
The first part of the command is
git config --global alias.remaster '...'
remaster
git remaster
The commands
Let's now focus on the commands between quotes
I will use the following schema to explain what's happening.
- remote repository : the git repo which is on Gitlab, Github...
- local repository : the git repo which is on your machine, containing your branches, your commits... Everything that has a meaning for git
- working directory : where you work, basically, it's the files and directory that you're seeing
- staging area : where your modifications go when you do a
git add
Before presenting the underlying effects, do you know what is
origin/master
git fetch
git pull
origin/master
That's why my sensei is saying that you can delete your
master
origin/master
post slashgear_/git-tip-why-you-should-not-keep-a-local-master-branch-3400
- we update
git fetch
, the copy of the remote master branchorigin/master
- : it removes untracked files. In other terms it removes files that have never been added to the staging area
git clean -f -d
- we want, our files (the working copy) to be in the same state as the
git reset --hard origin/master
branchorigin/master
There's one thing that could remain strange : the
!
!
git config alias.co = checkout
!
One small thing
If your main branch isn't
master
develop
You can use.
git config --global alias.nuke '!git fetch && git clean -d -f && git reset --hard origin/develop'
The two parts that can change are the
ALIAS_NAME
MAIN_BRANCH
git config --global alias.ALIAS_NAME '!git fetch && git clean -d -f && git reset --hard origin/MAIN_BRANCH'
It's maybe trivial but I prefere to point it out. 😉
Conclusion
The
git remaster
You can run this command everytime you screw a branch and you want to get back to a clean state.
Don't forget, time is too precious to reclone a repo. Hope it will be useful.
Thanks to Marvin and Émilie who've helped me with the english translation.
Thanks for reading.