.. I didn’t go through it, because my underlying assumption with explaining the inner workings of Git is that you’ve used it before. From time to time, I’ve broken that assumption to explain some things, so I guess your question is very valid.

This should help — http://rogerdudler.github.io/git-guide/ — but this also goes into pull and push pretty soon.

Okay here’s another example, just for you:

Let me know what isn’t clear.

$ mkdir basic-example
$ cd basic-example/
$ git init
Initialized empty Git repository in /Users/X/basic-example/.git/
$ git status
On branch master
No commits yetnothing to commit (create/copy files and use "git add" to track)$ vim hello_world
$ git status
On branch master
No commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
hello_worldnothing added to commit but untracked files present (use "git add" to track)$ git add hello_world
$ git commit -m "add basic hello world file"
[master (root-commit) 83635bb] add basic hello world file
1 file changed, 1 insertion(+)
create mode 100644 hello_world
$ git status
On branch master
nothing to commit, working tree clean

Now branching, creating a feature and merging it to master:

$ git checkout -b feature/bye-world
Switched to a new branch 'feature/bye-world'
$ vim bye_world
$ git status
On branch feature/bye-world
Untracked files:
(use "git add <file>..." to include in what will be committed)
bye_worldnothing added to commit but untracked files present (use "git add" to track)$ git add bye_world
$ git commit -m "add bye world"
[feature/bye-world f912735] add bye world
1 file changed, 2 insertions(+)
create mode 100644 bye_world
$ git status
On branch feature/bye-world
nothing to commit, working tree clean
$ git checkout master
Switched to branch 'master'
$ git status
On branch master
nothing to commit, working tree clean
$ ls
hello_world
$ git checkout feature/bye-world
Switched to branch 'feature/bye-world'
$ ls
bye_world hello_world
$ git checkout master
Switched to branch 'master'
$ git merge feature/bye-world
Updating 83635bb..f912735
Fast-forward
bye_world | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 bye_world
$ ls
bye_world hello_world

Now realising it’s crap and reverting master back:

Option 1: Create a new branch from old snapshot of master and merge as above-

$ git log
commit f9127350e66c536ac0611596b984e782336fd5d7 (HEAD -> master, feature/bye-world)
Author: Neil Kakkar
Date: Mon Dec 17 21:57:21 2018 +0000
add bye worldcommit 83635bbf33654880b8ed9f224eecab15f9b2500e
Author: Neil Kakkar
Date: Mon Dec 17 21:55:00 2018 +0000
add basic hello world file$ git checkout 83635bbf33654880b8ed9f224eecab15f9b2500e
Note: checking out '83635bbf33654880b8ed9f224eecab15f9b2500e'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>HEAD is now at 83635bb add basic hello world file$ git status
HEAD detached at 83635bb
nothing to commit, working tree clean
$ git checkout -b fix
Switched to a new branch 'fix'
$ git status
On branch fix
nothing to commit, working tree clean
$ ls
hello_world
$ git checkout master
$ git merge fix

Option 2: (Cleaner, but you lose history of the merge)

$ git log
commit f9127350e66c536ac0611596b984e782336fd5d7 (HEAD -> master, feature/bye-world)
Author: Neil Kakkar
Date: Mon Dec 17 21:57:21 2018 +0000
add bye worldcommit 83635bbf33654880b8ed9f224eecab15f9b2500e (fix)
Author: Neil Kakkar
Date: Mon Dec 17 21:55:00 2018 +0000
add basic hello world file$ git reset 83635bbf33654880b8ed9f224eecab15f9b2500e$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
bye_worldnothing added to commit but untracked files present (use "git add" to track)$ ls
bye_world hello_world

You can follow reset as explained in the article.

This helps? :)

Written by

I write about Code and Life philosophies. Sometimes both. | https://neilkakkar.com | Engineer @Bloomberg | Write (Code). Create. Recurse.

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