Git & GitHub tutorial
Git is a version control system for tracking changes in computer files. It was initially created by Linus Torvalds (creator of Linux system) in 2005.
We can use it to store any kinds of programs. It is distributed version control system. It means that many developers can work on the same project without being on the same network. It tracks each of the changes made to the files in the project. The user can revert to any file at any time of it had been committed to the repository. We can even look the snapshots of the code at any particular time in history. We can upload (or push) the files to the remote repository.
Initializing local git repository
Configuring the Username and Email
git config --global user.name 'utpalkumar'
git config --global user.email 'firstname.lastname@example.org'
We can have a look at the configuration using the following command
git config --list
If you need any help, you can just type:
Now, to add the files to the index and working tree (our final aim)
git add command
git add filename.py
To add all the files in the current directory
git add .
git add *.html
We can get the information about the tracked and untracked files. Tracked files are those which have been added to the working directory.
If we make any changes to the files, we can inspect the changes using the
If we want to remove the file from the index (untrack the file), then we can simply type:
git rm --cached filename.py
To remove the files from the index and the working tree:
git rm filename.txt
If we want to rename the file then we can do it using the git command too. We don’t need to untrack the file and then rename the original file and add it again
git mv filname.txt newfilename.txt
Now, we can commit the files to add to our repository on the GitHub.
There are two ways of doing that:
1. First way opens the vi or default directory on the local computer, and the user is prompted to enter the message. It is safe to enter meaningful messages because it is useful to track the changes made to the file.
2. The user can also enter the message using the -m flag
git commit -m 'made some changes'
To make changes to the committed files, the command is –amend
git commit --amend
If we don’t want to include some files in the current directory into the index or working tree, we can add the name of those files in the
We can obtain the log of the git actions
git log --pretty=oneline
We can make this better formatted
git log --pretty=format:"%h : %an : %ar : %s"
For all commits within a week
git log --since=1.weeks
For all commits since some given date
git log --since="2014-01-12"
All the commits of a given author
git log --author="utpalkumar"
All commits before a given date
git log --before="2014-04-30"
If we are a group of developers. We don’t wish to add any changes to the repository without finishing a particular sub-project. We can avoid that by working in a branch
To create a branch other the main branch (master)
git branch mybranch
To switch to the new branch
git checkout mybranch
We can make any change in this branch and merge these changes in the master branch, we need to first switch to the master
git checkout master
to merge the changes to the original file
git merge mybranch -m "added mybranch"
For adding files to the remote git repository, we need to make account on the Github website and then we need to start a new repository, name it, give some required details then, we can add files from our local repository to the remote repository using the following commands:
git remote add origin https://repositoryaddress.git
Replace the above fake URL with the URL you get from the repository you create on the Github website.
If we want to add the same files to another repository, we need to remove the added remove origin using the command
git remote rm origin # to remove the remote origin
We can download the git directory by simply using the
git clone command
git clone https://repositoryaddress.git