The git remaster command
Tags: git, eatsnobread, thug, nuke
Published: 2020-03-18 16:09:24

---
title: The git remaster command
published: true
description: Don't panic, git's ok.
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/dfetqx2ok9r641w9le07.png
canonical_url: https://maximeblanc.fr/blog/the-git-remaster-command
tags: git, eatsnobread, thug, nuke
---

OK, OK. This command doesn't exist. It's one of the aliases that I'm using.

## TL;DR

Execute this command once only.

```bash
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.

```bash
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](https://dev.to/emmabostian). I was doing the same until I've found a solution. Btw, you should follow her.

{% twitter 1232412585397571584 %}

In this part I will explain in detail the meaning of this command.

```bash
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](https://dev.to/jeansmaug/git-configuration-et-fourberies-2ajc) (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](https://dev-to-uploads.s3.amazonaws.com/i/n1r14o35uomerk1jcljk.png)

- **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".

{% post slashgear_/git-tip-why-you-should-not-keep-a-local-master-branch-3400 %}

- `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](https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases)

### One small thing

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

You can use.

```bash
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`.

```bash
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

- [Banner image](https://www.motherjones.com/kevin-drum/2019/06/one-third-of-americans-want-to-nuke-north-korea/)

- [Create alias with mutliple git commands](https://stackoverflow.com/questions/7534184/git-alias-multiple-commands-and-parameters)

- [Remove untracked files with git reset &rarr; git clean](https://stackoverflow.com/questions/4327708/git-reset-hard-head-leaves-untracked-files-behind)