5 git tips for beginners
Tags: git, beginners, webdev
Published: 2022-01-26 21:54:46

Practical commands for efficiency and a clean tree

## 1. Shortcuts (aka Alias)
You will find tons of alias ideas out there, the ones I found using the most are these. You can edit them using `nano ~/.gitconfig` in mac/linux.
So instead of typing `git checkout <branch>` you can type `git co <branch>`

```config
[alias]
   co = checkout
   br = branch
   st = status
   # create a new branch from current branch and check it out
   cob = checkout -b
   # stage all changes and commit (add a message)
   a = !git add -A && git commit -m
   unstage = reset HEAD --
   last = log -1 HEAD
   hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
   # Meld the currently unstaged changes into the last commit
   fixup = !git add -A && git commit --amend --no-edit
```
I will be using these throughout this article.


## 3. Bug and feature branches
From your main or dev branch use `git cob bug/123` to create a branch that will be used to work on issue #123 .. you can use `bug/123` or `issue/123` if you want to distinguish between bugs, tasks, features. This will organize them in folders when using git gui tools because of the forward slash.



## 4. Change your git editor to nano 
Unless you where born in the 1980s you likely hate vi or vim editor, you can change it with:
```bash
git config --global core.editor "nano"
```

_Use `Ctrl+o` to save (ie o for output), `Ctrl+x` to quit, `Ctrl+w` to search_


## 5. Clean up your commits
When creating a bug branch called say `bug/123` you will likely have tons of `wip` ie. work in progress commits. If you want to squash them all down to 1 commit before publishing the branch, then you can do this with `git rebase -i`
You want to go from this:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xnrucul13d3rnv7a2i1.png)
To this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06qsg3r24gvz03b4egd9.png)
 
```bash
# Open interactive rebase with the last 6 commits
git rebase -i HEAD~6 
```
That will open nano or vi with the ability to tell git what to do:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uw8fx7ejb2peftx2a2h8.png)

What we want is to squash all the commits together.
The top commit is the oldest one so you need to `pick` that one because `squash` will squash with the **previous** commit. If you don't one to squash them all onto that commit (maybe you wanted the next or previous one as the starting point, just exit and change the HEAD to HEAD~5 or ~7. _**Caution**: make sure to comment out all the lines with `#` so that the rebase does nothing!_


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1z1f8c3jva5mxmj0oae.png)
 
 Then save (`Ctrl+o` in nano, or `<Esc> :wq` in vim). It will then let you pick the commit message for that rebase, just comment out (`#`) all of them and type your own or just pick the best one.

**Tip**: If you have already published the branch (and all those noisy commits) you are working on, then rebasing it locally won't change it remotely. So delete the remote branch first `git push origin --delete bug/123` (assuming you use `origin` as name of your remote) then publish it after the rebase, or else the next time you `git pull` it will pull those commits back into your local branch.

🤗 _Thanks for reading. Open to feedback and other tips!_