My Most Used Git Commands
Tags: github, productivity, webdev
Published: 2019-05-21 17:28:03

This is not a full guide to which commands you should learn. I just find it inspiring to read about other developers’ habits and work routines. This I would like to contribute to and tell about _my most used Git commands_.

I honestly don’t have a specific way on typing these commands. I sometimes write the full command. I sometimes replace `git` with `g`. And then I sometimes write the full alias. I think it depends on the goal with the command. It is worth mentioning that I am using ZSH with [oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh).

## Main Commands

These are the commands that I use _every day_ - or almost every day.

```
git add --all
git commit -m “Add this commit”
git push
```

If you have heard about Git, then you will probably know these three classic commands. I use them pretty much all the time. It is here that it will be nice to use some aliases. `gaa`, `gcmsg “Add this commit”` and `gp` will come in handy!

```
git checkout develop
git checkout -b my-new-branch
```

These will switch to an existing branch or a new branch. There is not much new going on here. I often use the full aliases here too - `gcd` and `gcb “my-new-branch“`.

```
git pull --rebase
```

I always use the option `--rebase` when I pull from a repository. This will keep my commits nice and clean on the top of the tree. You could use the alias `gup`. It was a senior developer who taught me the power of rebase.

```
git rebase <branch>
```

This will lead us to rebase itself. _Remember to rebase!_ This is especially important if you are doing feature branches. We do not want a half-dead branch, which is a billion commits behind its default branch with merge conflicts up to the throat. _Ouch!_

```
git merge <branch>
```

It is not that often that I need to merge directly from my terminal. In my team at work we use a branching strategy with feature branches. When a branch should be merged into the default branch (or another), then we use a pull request from GitHub — and the GUI at the website is just fine.

```
git stash
git stash pop
git stash apply stash@{1}
```

Has your project manager given you a new task, which need to be done quickly? Just throw your current work to the side and focus on the new stuff. _It is awesome!_ I actually don’t use the full alias for this. I am using _g_ instead of _git_. Maybe it is because I want to be absolutely sure, that I am actually stashing 😀

```
git status -s
git log
```

These are the commands that keeps me updated, and I use them about 500 times a day. I use `gss`, `glg` or sometimes `glol`.

## Side Commands

These are the commands that I use occasionally.

```
git push --force-with-lease
```

This is one of my strange darlings. But why don’t I just use `--force`? First of all, it is an extremely dangerous command and a huge _no-no_ when using shared branches. It is because it will overwrite the remote repository with whatever you have locally. This can be dangerous if other contributors of the repository have pushed in the meantime. I have mostly used `--force-with-lease` after a rebase. This is because it works like a safety belt. [This article](https://blog.developer.atlassian.com/force-with-lease/) has a great example.

```
git for-each-ref — sort=-committerdate refs/heads/
```

This is actually a command, that I found a few months ago from [David Walsh](https://davidwalsh.name/sort-git-branches). The command will list the most recently worked on branches from top to bottom. _It is so cool!_

```
git reset --hard
```

And if everything goes like 💩, you can always reset the project.

Thank you for your time!

If you liked this, then please ❤️ and [*follow me on Twitter*](https://twitter.com/mrsteffenp).