Fork me on GitHub

Learning Git: 101

Muhammad Yasoob Ullah Khalid

Introduction

Git is a version control system. It allows you to manage your projects. You can freely edit the code of your projects without ever worrying about loosing anything!


In this tutorial you will learn about the basics of git. I will take you from installing git to merging two branches. Please bear with me. I am pretty sure that you will enjoy git after you get a hang of it!

Installation

Installation is very easy. Just type the following command on Ubuntu

sudo apt-get install git
Setup Global Variables

Git needs some innitial information before we can start working with it. You can supply this information with the commands written below. We use --global so that we don't have to input these commands evertime we innitialize a new git repository.

git config --global user.name 'Shaumik'
git config --global user.email 'sd@gmail.com'

Creating a new repository

In order to setup a new repository in a folder just type in the following command

git init .

This innitializes a new git repository in your current folder and allows you to use other git commands.

Checking status

You can check the status of your repository by typing the following command:

git status

This command shows you some helpful information about your repository. It tells you about which files are being tracked by git and which are not.

Let's add those files

If you have untracked files in your repository. You can easily tell git to track them by running the following command:

git add file_name

Just replace file__name with the name of the file you want git to track. If you want git to track all the files in the current repository then type:

git add .

Now it's time to commit

So now that you have told git to track the respective files you can commit those files to git:

git commit -m "Commit message"

You need to replace Commit message with the message you want to associate with the respective commit. The commit message should tell you the changes you have introduced in your repo in the respective commit.

Diff

So you have changed your files since your last commit and you want to see those changes. Git makes this easy for you. Just type in:

git diff

If you want to see the changes introduced to a specific file then simply type this (replace file_path with the path to the file):

git diff file_path

Where are the logs?

Do you want to see all of the commits which you have made to the repo? Just use this command:

git log

This command prints all of the commits which have been made to the repo. You will use git log a lot in future!

Let's stash 'em changes!

You have made some changes to the repo and want to save those changes. However you are not yet ready to make a full commit. What would you do? You would stash those changes!

git stash

Git records the changes you have made and saves them for later. Once you are ready to apply those changes just type the following command:

git stash apply

This command comes in use while moving from one branch to another. You will learn about branching later.

Let me show!

So you want to see the exact changes which you made in your previous commit. It's easy:

git show HEAD

If you want to see the changes done in n commit (n is the number of commits before head) then simply type this (replace n with a number):

git show HEAD~n

Note: HEAD is a reference to your latest commit

Branches. Branches everywhere.

So you want to make a LOT of changes to your project but don't want to modify your current working project. You have a solution and it is called branching. Currently we have been working in the main branch (master branch). You can create a branch in your repo and modify the code in that branch. If you are satisfied with your changes and they work perfectly you can merge that branch into your master branch. Otherwise you can simply delete the new branch and start over again.

You can create a new branch by typing this command:

git branch branch_name

Note: You are not automatically shifted to the new branch. I will share that command and the merge command in the next section.

Checkout.

This checkout command has a lot of usecases. We will take a look at them one-by-one.

You have made a lot of changes to a file and now you are regretting it. You want to change it back to the way it was after your last commit. What do you do? You checkout! (replace file_name with the path to the file)

git checkout HEAD file_name

This checkout command to move to another branch. After you have created your branch following the code from the section above, you can move to that branch by typing the following command:

git checkout branch_name

You can create and move to a new branch in a single command as well.

git checkout -b branch_name

Merge dem files!

So you have made some changes in a branch and want to move those changes over to the master branch. You can easily do that. Fist you need to move to the master branch

git checkout master

Then you need to type the following command. Replace branch_name with the name of the branch from where you want to pull the changes from.

git merge branch_name

Reset

Git reset is similar to when you checkout a file using the checkout command. The only difference is that when you reset a file, git removes that file from staging area but keep the changes in the working directory.

git reset HEAD file_name

The above command resets the file in the staging area to the way it was after your last commit. However you do not loose your edits. They are present in your directory but just not staged for the next commit.

You can also reset your project to a previous state using reset. First of all you need to find the SHA value of the commit till which you want to reset your project. You can easily find it using the git log command. After that you can type in the following command:

git reset 970c88e

Note: You only need the first 7 characters of the SHA

GitHub

This is the website which is dedicated to git and social coding. I am going to share some commands involving github with you.

Fork

You can fork another repository of GitHub by going to a repository page and clicking on fork at the top. This will create a copy of that project under your GitHub account.

Cloning

You can then create a local copy of the remote repo by typing th following command: (You can find your clone url by opening your repo on GitHub and looking at top-right corner)

git clone git@github.com:yasoob/learn-git.git

The above command will create a local copy of the remote repo. You can make changes to it and then after committing those changes to the local repository you can push them to the remote repository.

git push origin master
Rebase

This command has a lot of purposes but I will show you the most-used one. Suppose you have made a LOT of similar commits into a repository while testing some code and now you want to merge those commits into one. You can do that by doing a git rebase. It is easy. Just type: (replacing n with the amount of last commits you want to modify)

git rebase -i HEAD~n

This will present you with something of this sort.

pick 424ad46 branching
pick 970c88e checkout, merge
pick d7c8d36 reset
pick c1d129c github

# Rebase 5fc27a2..c1d129c onto 5fc27a2
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
        

If you want to squash (merge) these 4 commits into a single commit then you have to change the last three picks to s. It would become something like this:

pick 424ad46 branching
s 970c88e checkout, merge
s d7c8d36 reset
s c1d129c github

# Rebase 5fc27a2..c1d129c onto 5fc27a2
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
        

Now you can save the file and you will be presented with a screen where you can create a new commit message.

Force Push

If you want to push a rebased repo online then you have to do something a bit different. You have to do a force push. It can be done like this:

git push origin +master

The + sign tells git to force push the repo. Alternatively you can also do this:

git push -u origin master

And that's it! I hope you enjoyed this short intro to the world of git :)

Goodbye!

This tutorial was made for FOSSASIA. It is an organization of awesome people. You can check them out if you ever feel like contributing to the world of Open Source.