Making sense of Git and GitHub - III [Reset, Branching]

This post is in continuation of this post.

Removing a file from the Staging area:
Staging = Cached = Index.

What command do we use?

If you need to remove only from the Staging area, use git rm --cached <filename>.

If you need to remove from both the Staging area and the Working directory,

use git rm -f <filename>.


Note: Git repo doesn't have any impact and still contains File3.txt.


Removing in Local repo:

The local repo gets a copy from the staging area. Therefore removing or resetting a specific commit, we need to consider the staging area copy.

The command is git reset <mode> <commit_id>.

The important modes are,

  • --mixed(default one)

  • --soft

  • --hard

--mixed (Both staging and Repo copy removed)

--soft(Only Repo copy removed)

--hard(Working, Staging and Repo copy removed)

When you reset the commit, all other commits committed after that will be removed.

Division of labor:

If you want to implement two features on your app within a short span, how will you do it?


The constraints would be,

The existing code should be unaltered or progressed, irrespective of the feature's code.

The feature's code is an add-on over the existing app code.

The feature's code must be joined to the existing code at the end.


Welcome to git branching!


When no branch is created, git will put you in the master branch. It is also called the main branch.


Note: On branch master.



When you give 'git branch,' you get which branch you are on by prefixing '*.'



If git creates and puts you on a branch by default, you can also create and switch between branches, right!

You can create two new branches for feature1 and feature2, and you work on those branches independently of one another and the master branch. Well! That's a division of labor for you from git's perspective!


Creating a git branch

We create a branch from another branch. Therefore the first branch is made from the default branch(master).

When the branch is created, it inherits all the contents from the parent branch.

You need to make changes and commit them separately. They are independent of one another.

Confusing, let me illustrate,

Note: Branch is created in git and is treated from a git repo perspective.

Hands-on:

We will do the above illustration in git.

Initialize git init.

Add and commit File1.txt.

Give git branch now,

$ git branch
* master

So we are in the master branch(default created by git).

But we need to create the first branch from this commit.

What is the command? git branch <branchname> --> git branch Branch1.

This creates Branch1.

Which branch are we on? Type git branch.

$ git branch
  Branch1
* master

Notice Branch1 is created, but we are on the master branch(*).


We will move to Branch1.

What is the command? git checkout <branchname> --> git checkout Branch1

$ git checkout Branch1
Switched to branch 'Branch1'

Type git branch,

* Branch1
  master

So we are on Branch1 now(*).

We will create x.txt and y.txt in this branch.

Type git status now,

$ git status
On branch Branch1
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        x.txt
        y.txt

nothing added to commit but untracked files present (use "git add" to track)

We will add and commit both files in Branch1.

Okay, where is file1.txt inherited from the master branch?

Type 'ls' to show files in the working directory,

$ ls
File1.txt  x.txt  y.txt

Yes, we got File1.txt along with x and y.

What about the staging area? Type git ls-files.

$ git ls-files
File1.txt
x.txt
y.txt

We will move back to the master branch.

Type git checkout master.

We will create File2.txt and commit it.

Type 'ls' and 'git ls-files'.

$ ls
File1.txt  File2.txt
$ git ls-files
File1.txt
File2.txt

Have you noticed the working directory and staging area change as the branch changes?

Therefore, the Working and staging area depends on the branch.

We will create Branch2 from here.

But this time, we will create and switch with a single command.

The command is,

git checkout -b Branch2

$ git checkout -b Branch2
Switched to a new branch 'Branch2'

We will create a and b and commit in Branch2.

Now give ls and git ls-files,

$ ls
File1.txt  File2.txt  a.txt  b.txt
$ git ls-files
File1.txt
File2.txt
a.txt
b.txt

We will checkout the master branch now and create File3.txt


Hence we completed the hands-on of the above illustration.