Git basics

Git

Git is a version control tool. It helps you track the versions of your project as you make changes.

Note: Github is a platform that allows developer to create and manage their code. It uses Git and some other stuff like access control, bug tracking, etc.

We first show how we can use Gitand we learn some basic commands. Then, we show how we can use it with Github.

Example

Say you want to write a program, and you may want to keep track of changes. So, let’s move to a empty directory, let’s call it data-science-realm. Here we now create an empty git-enabled (sub)directory that allows us to do version control over its files.

Run this command:

git init <Name-of-Directory>


This will initialize a git-enabled directory under the current directory.

Note: If you create a repository on Github and clone it so that you can work on it locally, a git-enabled directory whose name is the same as the name of repo will be created during the cloning process, and it is git-enabled! So, no need to run git init ... there. Once we get to the Github part, I will explain how things are similar/different.

At a very high-level, this is how Git works. It starts with a main (or master in older versions) branch. From there, person X can create a new branch B1 and works on some stuff. At the same time, person Y can create another new branch B2 (off the main branch) and starts working on some other stuff.

A _ B1
  \ B2 

Note: In case you are wondering “why not just copy it?”: Although you can copy the content of directory and start working on your stuff, it can become difficult to update the project by merging it to main. Furtherfore, you will not be able to track your own changes easily later.

Let’s say you are in the main branch, and you want to create a new branch called new-branch. To achieve this, run the following command:

git checkout -b new-branch

This will create the new branch new-branch and you are already moved to this branch. So, whatever changes you make on your files will be on this branch. Let’s create a file called test_file.txt and write hello world! in it. Let’s save and close it. Now try to run git status in the terminal. You will see the file is untracked. Now, if you switch branches, you can still see the untracked file. To add this file to that branch, you can do: git add test_file.txt. This will put file in the “staged” mode. This means the file is ready to be “commit”-ed to the branch. You can have several files in the “staged” mode, and finalize those changes by one commit. To commit, run: git commit -m "MY COMMIT MESSAGE". The change you made is now commited to the branch. If you do git log --oneline, you should be able to see a list of commits, where the latest one is the one you commited recently. You can use the commit id, and do something like: git show <COMMIT-ID> to see it. But, let’s say you forgot to add another change and you prefer to have one commit for both rather than creating a new commit. SO, you can do: git reset --soft HEAD~1. This will reset the latest commit by brining it back to the “staged” mode. You can then do git add <NEW-CHANGED-FILE> and brings that file to the “staged” mode as well, and then do: git commit -m <COMMIT MESSAGE>. What if you decide to completely disregard your latest commit? It can easily be done by running: git reset --hard HEAD~1.

There are other few things that I am going to just name it here: * git branch -a or git branch -l * git merge <from-branch> * git diff <File_Name> or git diff --staged <File-Name>

Some resources to learn git

  • https://learngitbranching.js.org
  • https://ohshitgit.com
  • http://up1.github.io/git-guide/index.html
  • https://gitimmersion.com/index.html
  • Udemy git course