Introduction to Git and Github
By: Prateek Agarwal
Tags: Git, Github, Version Control System

You have successfully configured Git and are good to go!

Git Commands

Make a new directory (aka folder)- mkdir foldername Move into that folder- cd foldername Make a new file say testfile.txt- touch testfile.txt. The ‘touch’ command is used to create a new file. Here we created a text file with name ‘testfile’. Open the file in some text editor installed on your PC-

For Notepad use- notepad.exe testfile.txt

For Visual Studio Code use- code testfile.txt

For Atom use- atom testfile.txt

For Sublime Editor use- subl testfile.txt

5. Add some text in this file and save it and then go to the terminal and use Ctrl+C to exit the command and enter a new line.

Now let's actually start implementing some Git Commands!!

1) git init

This would initialize a git repository in your ‘git_intro’ folder i.e convert your normal directory ‘git_intro’ into a Git Repository.

A git repository is basically a hidden folder “.git”, which is required to log every commit history and every other information required for your repository, version control, commits, etc. Just think that this hidden folder stores data about your project like its history, past versions, the latest changes that you have made, etc.

git init : Transform the current directory into a git repository.

: Transform the current directory into a git repository. git init <repo_name>: Makes a directory with the name repo_name and converts it into a git repository.

See the (master) at the end? This tells you that you are working in a folder tracked by Git and that you are currently in the ‘master’ branch of your project.

2) git status

This command returns the current state of the repository. ‘git status’ will return the current working branch. If a file is in the staging area, but not committed, it shows with git status. Or, if there are no changes it’ll return nothing to commit i.e. working directory is clean.

Here, it tells us that:

We are on branch ‘master’ (written in blue colour).

We have an untracked file (written in red colour).

3) git add

This adds untracked files to the staging area for Git. Before a file is available to commit to a repository, the file needs to be added to the Git index (staging area). There are a few different ways to use git add, by adding entire directories, specific files, or all unstaged files.

To ‘add’ all the files in a git-tracked folder to the staging area, do the following: git add .

If we wanted to add just one certain file to the staging area, we could have done: git add filename.txt

Now, our file is in the staging area. If we hit the command ‘git status’ now, we get the following output:

It tells us that we have a new file in the staging area, and that it is ready to be committed now. It also gives us a command to unstage (remove from staging area) our file which we just staged. Now let us finally commit our file!

4) git commit

Git commit command takes a snapshot representing the staged changes, i.e. it commits the staged files in our git repository.

After running the git commit command, you need to type in the description of the commit in the text editor, aka. the commit “message.” Or, you can do it in one go by using the message tag (-m).

The commit message is supposed to be a short, crisp, and clear description of the work that is being committed. It is also a convention to keep the commit message in the present tense. A meaningful commit message in our case would be:

5) git diff

This command shows the file differences which are not yet staged.

Let us make some changes in our testfile.txt file which we just committed. Let us write more in that file and see what git diff tells us.

Save the file. Then in the terminal, enter the git diff command:

Voila! Git noticed that we have made a change in our file, and it even shows us which line was modified! That is pretty awesome!

Let us stage our recently modified file by using git add. Now, a git diff command would give no output now, since we have already staged our file, and there are no differences between the staged version and the current version of the file.

Now, to see the differences between a staged file and the previous version of the file (which was committed,) we use the same git diff command, but with a --staged flag at the end.

Using git status now would tell us that the staging version of the file and committed version of the file have some differences.

6) git reset

To unstage an already staged file, we use the command git reset. It would bring the file back to its previous state, which is, with untracked changes.

Run git status and git diff commands. These two commands state that our file was unstaged, and now is modified, and those changes have not been staged yet:

git reset helps to undo repository to any particular state present in the history of the repository.

Soft Reset: Resetting repository to a given commit and adds all the changes to the staging area. Command: git reset --soft

Resetting repository to a given commit and adds all the changes to the staging area. Command: Hard Reset: Resetting repository to a given commit and deleting all the changes. Command: git reset --hard

7) git log

This command is used to list the version history for the current branch. This would tell us about all the commits that have been made in the project.

Let us ‘git add’ and ‘git commit’ our file and then see what git log shows:

Wonderful! git log clearly shows us that we have made two commits in our project, who made them, when they were made and also the commit messages that we added! There it is! Our mini project’s version history!

View a complete and descriptive commit history: git log

To view commit history in a summarized form: git log --oneline

To view commits related to a file: git log --oneline <file_name>

git log --since=”1 month ago”

8) git branch

git branch operates branching operations such as list, delete and creates branches in the repository.

git branch: This command written in a working repo displays the local branches associated with the repo and the branch in green is the current working branch.

This command written in a working repo displays the local branches associated with the repo and the branch in is the current working branch. git branch <branch_name>: This creates a new branch branch_name locally sourced from previous branch.

This creates a new branch branch_name locally sourced from previous branch. git checkout <branch_name>: To go to the branch branch_name.

To go to the branch branch_name. git checkout -b <branch_name>: The above two steps can be performed using this single step. b is an alias for branch

Note: All the changes have to be commited before moving to a new branch.

git branch -d <branch_name>: it deletes a branch that is no longer needed. Git will warn you if you are deleting an unmerged branch.

Let’s add a file in our newly created branch and commit the changes in it:

9) git merge

Once you’ve made isolated changes in a branch and tested, and are confident to merge the changes in the some branch say master, you can merge. The merge command always merges into the currently checked out branch.

Github Introduction & Features

Until now, we have been working only in the local repository. Each developer will work in their local repository but eventually, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code.

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere in the world. Git is essentially at the heart of GitHub.

Git is a version control system which tracks the changes when working with computer codes while GitHub is a Web-based Git version control repository hosting service. It provides all of the distributed version control and source code management (SCM) functionalities of Git while topping it with a few of its own features.

Git != GitHub

GitHub adds extra functionalities on top of git, such as

Great UI

Pull Requests

Documentation

Bug Tracking

Feature Requests and many more…

You do not need GitHub to use git but to use github you can use git.

10) git clone

If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to, or would like to make your own — the command you need is git clone.

Open a fresh Git Bash terminal from your PC.