Mastering git branches

What is a git branch?

image showing 3 branches

But how can it be so simple?

image showing what is a branch reference

But wait… How it’s possible to be just a pointer?

image showing how a commit references a ancestor

What other characteristics does a branch reference has?

> git checkout branch-1
> echo "add one more line" >> existing_file.something
> git add existing_file.something
> git commit -m "Add a change"
  • “Hey go to branch-1”
  • “Add one more line to an existing file”
  • “Put that changes in staging area”
  • “Given that we are in branch-1, commit the changes in stage and move the branch-1 reference to the new commit”
> git checkout branch-1
> git reset --hard HEAD~1

Using git rebase

  • Rewrite a commit message, plus squash, remove or reorganise commits. This is when you want intentionally change the history within the deviation point to the branch reference, the same is to say within the entire branch.
  • Change the point where the branch started, the deviation point. Now as you going to see when we change the deviation point, all history must change because the starting point is different.
this is the scenario we are going to start

Changing the git base branch of branch-1

> git log --pretty --graph --oneline --all* 34d036a (HEAD -> branch-1) 7
* 982b9cc 5

| * cde59d7 (master) 8
| * 4323998 6
|/
* 98e63b8 4
* b9cb989 3
* adbf8d4 2
* d42dc27 1
> git checkout branch-1
> git rebase master
> git log --pretty --graph --oneline --all
* eee7e1f (HEAD -> branch-1) 7
* a9fd961 5

* cde59d7 (master) 8
* 4323998 6
* 98e63b8 4
* b9cb989 3
* adbf8d4 2
* d42dc27 1

Rewriting the git branch history

> git log --pretty --graph --oneline --all* 34d036a (branch-1) 7
* 982b9cc 5

| * cde59d7 (HEAD -> master) 8
| * 4323998 6
|/
* 98e63b8 4
* b9cb989 3
* adbf8d4 2
* d42dc27 1
> git checkout branch-1
> git rebase -i 98e63b8
pick 982b9cc 5
pick 34d036a 7
  • r/reword, maintain the commit but change the message
  • e/edit, use the commit but stop for amending
  • s/squash, melt into previous commit
  • f/fixup, like “squash” but discard this commit log
  • x/exec, run rebase in the shell from here
  • d/drop, remove commit
r 982b9cc 5
pick 34d036a 7
20
* ced67cf (HEAD -> branch-1) 7
* 165bf17 20

| * cde59d7 (master) 8
| * 4323998 6
|/
* 98e63b8 4
* b9cb989 3
* adbf8d4 2
* d42dc27 1

Using git merge

this is the scenario to rebase
git graph after rebase of branch-1 on the top of master
> git checkout master
> git merge branch-1 -ff
git graph after merge fast-forward
this is the scenario to rebase
> git merge master --ff-only
fatal: Not possible to fast-forward, aborting.
> git merge master
> git log --pretty --graph --oneline --all
* 9a4eb5c (HEAD -> branch-1) Merge branch 'master' into branch-1
|\
| * cde59d7 (master) 8
| * 4323998 6
* | 34d036a 7
* | 982b9cc 5
|/
* 98e63b8 4
* b9cb989 3
* adbf8d4 2
* d42dc27 1
merge without fast-forward

git merge vs git rebase

--

--

--

Software engineer at uniplaces

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

5 mins deploy hello world to kubernetes with Minikube on Mac

This Week I Learned #6

23. — 29. April 2018

Tips for those Teaching Themselves Programming

Flexbox and CSS Grid

SQL Databases — Injection Attack Series

Crafting the Perfect Git Commit

:nth-of-type vs :nth-child

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Henrique Mota

Henrique Mota

Software engineer at uniplaces

More from Medium

How to improve your code readability? Useful clean code ideas to know

Git Repository Clean Up

Understand how to use Git pull requests in 1 minute.

Obscure git Commands