Git and GitHub for newbies

I admit that I never really had too much of a liking towards Git. I knew it was this magical piece of software that was created so that you could easily manage your coding projects. But the sheer number of crazy tutorials and documentations were a big turn-off for someone like me. I still went ahead and created an account back in 2013 because all the cool coder kids had one! During the past couple of years, I have barely scratched the surface in using Git. I merely managed to get a few basic things up and running.

But today, I have decided to put my fears aside and learn Git for real. And since I have a terrible memory (and don’t want to keep hopping between the numerous help sites), I have decided to create a blog post (as you can see) of all the commands that I come across along the way. So watch out for more updates to the command list from time-to-time.

All the instructions below have been tested on Linux.


In case you need quick assistance with any command, say the merge command, you can quickly do

$ man git-merge


git help merge


$ git config --global "Your Name Comes Here"
$ git config --global

You can view the configuration list using

$ git config --list

Importing a new project

Let’s say you have a project you want to place under the Git revision control system. And for total lack of creativity, let’s call it ‘project’.

$ cd project
$ git init

You have now initialized an empty repository in your project folder. Git will take a snapshot of all the content of ‘project’ via the following command.

$ git add .

This stores the snapshot in a temporary area called “index”. You can store it permanently by following up the above command with

$ git commit

This will permanently commit the snapshot. You would also have to enter a commit message. A good practice is to start with a one line heading, followed by a blank line and then a detailed description. This is because git-format-patch turns a commit into an email with the top line as subject and the lines after the blank line as subject.

Making changes

If you modify file1 and file2, you can do

$ git add file1 file2

Or if you want to add changes made to all files, you can simply do

$ git add .

Before committing, if you want to see what changes you have made, you can execute

$ git diff --cached


$ git status

You can finally commit your changes using

$ git commit

Also, note that in case you hadn’t added any new files but had just modified existing files, you can directly commit the modified files using

$ git commit -a

Managing local and remote changes

Local to Remote

A local repository is the one on your desktop. A remote repository is the one on GitHub. I followed this Github tutorial for adding the existing local project to GitHub.

If you now want to add this project to GitHub, first create a new repository in GitHub, without creating a Readme or gitgignore files.

$ git remote add origin remote repository URL

Here you are specifying the origin as the remote URL that points to the location where your GitHub repository sits. You can then verify the remote URL.

$ git remote -v

Push the changed from local to remote using

$ git push origin master

Remote to Local

In case you made a few changes in the remote repository and want them to be reflected in your local repository, you can do a pull.

$ git pull origin master

This command is a combination of fetch and merge. It fetches all the data from the remote repo, (in this case, the master branch) and merges it with the local repo.

The fetch command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet. The merge command merges two or more development histories together. I haven’t really gotten to using fetch and merge till now, so I’ll update the blog later when I find out more about them.

Creating a gitignore file

Many a times, it becomes necessary to prevent certain files from being pushed onto git. These may include system files, virtual environment files, etc which you don’t want in your git repository. For precisely this purpose, git introduced gitignore. This involves creating a file in your repository called ‘.gitignore’ and in it, write down the files and folders that you want git to ignore.

touch .gitignore

Note that by default, this file would be hidden. Use an ‘ls -a’ command to check that it has been created. You could refer this list for the possible contents of this file. But note that once git has tracked the file, it can’t be ignored by just adding it in gitignore. That will need more elaborate steps that has not been detailed here. Also, don’t forget to commit the git file so that the changes can take effect from the next commit onwards.

More generally, if you want git to ignore a file called ‘ignorefile.txt’ and a folder called ‘ignorefolder’. You can just add the following to ‘.gitignore’

# file

# folder

Removing a file from git

In case you want to delete a file called ‘del.txt’, just use the command

git rm del.txt
git commit -m "Removed del.txt"
git push origin master

Managing changes

To create a new branch called ‘experimental’, do

$ git branch experimental

If you now run

$ git branch

you’ll get a list of all existing branches:

* master

The “experimental” branch is the one you just created, and the “master” branch is a default branch that was created for you automatically. The asterisk marks the branch you are currently on; type

$ git checkout experimental

to switch to the experimental branch. Now edit a file, commit the change, and switch back to the master branch:

(edit file)
$ git commit -a
$ git checkout master

Check that the change you made is no longer visible, since it was made on the experimental branch and you’re back on the master branch.

You can make a different change on the master branch:

(edit file)
$ git commit -a

at this point the two branches have diverged, with different changes made in each. To merge the changes made in experimental into master, run

$ git merge experimental

If the changes don’t conflict, you’re done. If there are conflicts, markers will be left in the problematic files showing the conflict;

$ git diff

will show this. Once you’ve edited the files to resolve the conflicts,

$ git commit -a

will commit the result of the merge. Finally,

$ gitk

will show a nice graphical representation of the resulting history.

At this point you could delete the experimental branch with

$ git branch -d experimental

This command ensures that the changes in the experimental branch are already in the current branch.

If you develop on a branch crazy-idea, then regret it, you can always delete the branch with

$ git branch -D crazy-idea

Branches are cheap and easy, so this is a good way to try something out.

Doing away with commits – git stash

Sometimes you decide not to commit any of the changes that you made. In cases where you want to drop all unstaged changes and return back to HEAD, you can just stash all the changes and then drop them.

$git stash save --keep-index
$git stash drop

I have used a number of sources for understanding the above commands. The most notable one being There are also a ton of answers on Stackoverflow in case you get stuck on any command or run into any errors. There’s a wonderful community out there, so make the most of it 🙂 

Forking and Merging from original

I found this wonderful answer on Stack Overflow which explains how to selectively merge commits from another forked repository.

Undoing last commit

force push changes to remote:

Merging multiple commits into one

Also, if you have done these merges locally, you’ll have to force push the changes to master (

$ git push origin branch-name --force

Renaming files

mv  <old name>  <new name>

Git Clone contents of a repo (without the folder itself)

git init
git remote add origin
git pull origin master

Commit empty folders to git

Another way to make a directory stay empty (in the repository) is to create a .gitignore file inside that directory that contains four lines:

# Ignore everything in this directory
# Except this file




One comment

  1. I pay a visit daily a few web sites and websites to read articles,
    but this weblog presents quality based articles.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: