DEV Community

Cover image for The git remaster command
jean-smaug
jean-smaug

Posted on • Edited on • Originally published at maximeblanc.fr

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.

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) and secondary the meaning of the created alias (the part between quotes).

The alias

Alias is just "a shorter name for something that is normally long to write". For example in maths you're using the = sign instead of writing is equal to.

The first part of the command is git config --global alias.remaster '...'. It tells Git : "Dear Git, please create an alias named remaster in the global config. After that, everytime I type git remaster I want the command between quotes to be executed." Learn more about git configuration (it's in french 🥖🍷🇫🇷).

The commands

Let's now focus on the commands between quotes

I will use the following schema to explain what's happening.

git-schema

  • 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 ? It's a branch ! It's a copy of the remote master branch. It's updated if there are new commits appearing when you do a git fetch or a git pull. This means that the origin/master branch is safe and that we can trust it.

That's why my sensei is saying that you can delete your master branch because you already have origin/master which is the "always clean master".

  • git fetch we update origin/master, the copy of the remote master branch
  • git clean -f -d : it removes untracked files. In other terms it removes files that have never been added to the staging area
  • git reset --hard origin/master we want, our files (the working copy) to be in the same state as the origin/master branch

There's one thing that could remain strange : the ! at the beginning.
The ! is necessary because I'm aliasing a bash command. It's not required if you want to alias a git command. For example you can write git config alias.co = checkout without !. More informations here

One small thing

If your main branch isn't master but develop, for example.

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 and the 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 command is just an alias. It brings all the modifications from the remote repository into your local repository.
After that, you need to bring these modifications into your working directory.

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.

Links

Top comments (2)

Collapse
 
terkwood profile image
Felix Terkhorn

Lovely. If you don't turn your local repo into a Gordian Knot once in a while, you're not trying hard enough 😂

Collapse
 
jeansmaug profile image
jean-smaug

Thx :D

Until I discovered how it was working internally, Git was something painful 😅
But now I find it wonderful !