Git it! - Simplified Git Tutorial (Part 2).

It's Commit'o'Clock! WTF is Github?

Posted by Aashish Kumar on May 06, 2020 · 14 mins read

If there is a tool or software that every software developer must know, it is GIT. Everyone will agree that we all struggle with it initially when we start learning it. So I’m here to ease the burden from the shoulders of the beginners, who are just starting with this amazing tool.

Let’s Git it!

We’ll be learning the following topics today:-

  1. Understanding Git (How it Works?)
  2. Installation and Setup
  3. How to Commit!
  4. Git + Github
  5. Branching and Merging
  6. Rebasing
  7. Squashing

Note :- First two topics are included in part 1. This is the part 2 and contains the next two topics, for later topics, head to part 3.

How to commit!

Now we’ll be assuming that you’ve successfully installed git and have it up and running. So as you’ve set up an empty git repository in the folder, and if you haven’t, just run the following command:

git init

This will initialize an empty git repository and a .git folder will be created. Now as we’ve initialized a repo, you need to add the files that need to be tracked by git. For reference we’ve created some temporary files, following is the file structure:

├── file1.txt
├── file2.txt
├── .git
└── temp_folder
    └── file3.txt

Each file contains text as “I am fileX.txt”, where X is the file number we’re in.

Now first run the status command to know the status of the git repository.

git status

The output is:

On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  file1.txt
  file2.txt
  temp_folder/

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

The output shows that all the files and folders are untracked and can be added to the repository, also we’re currently on the default master branch. So to add all the files for tracking, run the following command:

git add .

where . means that add all the files/folders in the current directory for tracking, you can specify the name of a specific file or directory which you want to track instead of the ., but we’re adding all files. Now again run the status command git status.

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

  new file:   file1.txt
  new file:   file2.txt
  new file:   temp_folder/file3.txt

Now the output shows that git has tracked all the new files and they have changes that need to be committed. So now you’re ready to make your first commit and save the changes by creating your first checkpoint. Run the following command:

git commit -am "My first commit"

Here -a flag means to take snapshot of all the changes in the current working directory and -m flag is used to pass the commit description or message in the command itself. If you don’t use the -m flag then git will automatically open a text editor, in which you had to write the commit message or description.

The output:

[master (root-commit) 2282d7f] My first commit
 3 files changed, 3 insertions(+)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
 create mode 100644 temp_folder/file3.txt

Kaboom! Congrats on your first commit. Run the git status command to check the status.

On branch master
nothing to commit, working directory clean

It says that now you’ve no changes left to commit and you’ve successfully created your first checkpoint. Now to check the commit log, run the log command:

git log

This command lists all the commits that you’ve made till now:

commit 2282d7ff037132a3c390518c82d776d0b86f6a29
Author: Aashish Kumar <aashish.kumar@devbae.tech>
Date:   Wed May 6 04:08:05 2020 +0530

    My first commit

The string of random words on the top is called the commit hash, which is like a unique id given to each commit to differentiate between two commits.

Now if you make any changes in any of the tracked files and run the git status command, you’ll be able to see the changes being actively tracked and when you commit those changes, they’ll be automatically saved.

As you now know how to commit, go make some changes, make commits and play around with committing (I’d suggest at least make 3-4 more commits). Always remember the three steps - make changes in files, add those files for tracking and commit them, also try to give unique commit message every time, describing the changes that you’ve made.

Hopefully, you would’ve created a few commits/checkpoints by now. So to revisit a particular checkpoint, you need to first run the git log command:

commit f181e0eeb09b72b6419f07baa51f1b0e2c13d5bc
Author: Aashish Kumar <aashish.kumar@devbae.tech>
Date:   Wed May 6 04:49:09 2020 +0530

    Deleted file3

commit 151c1b257e1214290e51f189d99a2faf99901afa
Author: Aashish Kumar <aashish.kumar@devbae.tech>
Date:   Wed May 6 04:48:40 2020 +0530

    Changed file2

commit 2bca72097fd1f8aa7efb436202a336cabcd9fc1b
Author: Aashish Kumar <aashish.kumar@devbae.tech>
Date:   Wed May 6 04:48:08 2020 +0530

    Changed file1

commit 2282d7ff037132a3c390518c82d776d0b86f6a29
Author: Aashish Kumar <aashish.kumar@devbae.tech>
Date:   Wed May 6 04:08:05 2020 +0530

    My first commit

This shows all my commits with their unique commit hashes and description. Now for e.g, you want to revisit the 2nd commit i.e you want your project to be in the state when the 2nd commit was made, just copy the commit hash of the 2nd commit and run the following command:

git checkout 2bca72097fd1f8aa7efb436202a336cabcd9fc1b

Now if you check your files, you’ll see you’re in the same state as when you made the 2nd commit i.e in my case file3 will exist and file2 would not have any changes as I made those commits later. Now to get back to the present state, run the following command.

git checkout master

This will take the HEAD to its original state.

For e.g, if you’ve recently committed and then again made some unnecessary changes, now you don’t want to commit these changes and want to go the state you were in when you made the last commit. You can simply reject these changes by running the following command, this is called stashing:

git stash

This will reject all the changes you made after the last commit and make your working directory clean again.

Now you know the basic working of git. It’s time to welcome Github and know it well.

Git + Github = fire_emoji :p

Now to share your git repositories, you need a server that can host your repos, so that you can easily share them with others. Github does exactly that, a server to save git repositories with a very cool and easy to understand web-based graphical interface, it also provides you various collaboration features for your projects like wikis, etc. Now we’ll be learning how to host your git repo on Github.

The first thing that you need to do is signup on github and create an account. Now you need to update your account details on your locally installed git first. Run the following commands:

git config --global user.name "<You Github Username>"
git config --global user.email "<Your Github Email>"

Creating a repository on Github

Login to your Github account, select Create New Repository, and select the following options:

image alt ><

Hit the Create Repository button.

Pushing local Git repo to Github

After creating the repo, run the commands as shown on the screen, use the link shown in your repo:

image alt ><

These are the commands:

git remote add origin <copied link>
git push -u origin master

Here origin is the name of the remote that we’re adding. The first command adds your remote repo to the local one, and the push command uploads your master branch to the remote repo.

You can add multiple remote repos with the following command:

git remote add <remoteName> <remoteLink>

Similarly, you can push multiple branches to remote servers by using the following command:

git push -u <remoteName> <branchName>

If you make changes manually on the remote repo on GitHub using its web interface, you need to update your local repository by pulling those changes, which can be done by the following command:

git pull <remoteName> <branchName>
git pull origin master # in our case

Now create your project, do multiple commits, push those commits on GitHub, and build a great GitHub profile <3.

Booyah! You’ve completed Part 2. How does it feel to be a git expert huh? Let us know in the comments below.

Now for the Advanced git topics. Head to Part 3. You can also check Part 1, if you want to learn about basic theory of git.

Note:- If you think there is anything wrong with the tutorial, or you can modify something, feel free to point out in the comments or raise an issue.