5 git tips for beginners

omills

Olivier

Posted on January 26, 2022

5 git tips for beginners

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>

[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
Enter fullscreen mode Exit fullscreen mode

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:

git config --global core.editor "nano"
Enter fullscreen mode Exit fullscreen mode

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
To this:

Image description

# Open interactive rebase with the last 6 commits
git rebase -i HEAD~6 
Enter fullscreen mode Exit fullscreen mode

That will open nano or vi with the ability to tell git what to do:
Image description

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

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!

💖 💪 🙅 🚩
omills
Olivier

Posted on January 26, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related