Introduction to Git and GitHub

The way I see it the most important skill a developer must have is knowing how to google stuff properly. This talent can only be achieved with practice. The next thing is understanding Version Control.

What is Version Control?

Version control (revision control or source control), is a system that keeps track of all the changes made to a file and classifies them as a version by giving them a different timestamp so that you can undo or redo any steps as you wish to any specific period of development of your project. In simple terms, it means that if you mess up any part of your code or you end up deleting any file which you later realise you did need you can still go back in time using version control.

Some of the famous Version Control Systems are Git, Apache Subversion(SVN), Mercurial Concurrent Version System(CVS), Perforce Helix, etc.

The VSC that I use is Git, and The hosting-service is GitHub. So let’s talk about that.

The difference

Git is the VCS. It is a tool that manages the source code history and allows one to go to and forth on the changes made to them.

Github allows hosting of git repositories on remote servers.

Now that’s out of the way lets get right into it already…

How to install Git ?

On Linux

use the following commands to install git

$ sudo apt update
$ sudo apt install git
$ git --version

On windows

The process is pretty simple you can download the latest version of git from

https://git-scm.com/download/win

Open the file and follow the instruction that appears and you’ll be all ready to go.

On Mac

There are two ways

  1. open terminal and type
git --version 

If you don’t already have git installed a prompt asking you to install it should pop-up accept it and that should do it.

2. Go to

https://git-scm.com/download/mac

Download the package
once it is done the when you open the file, the installation should begin, follow the process and wholla.. you have git installed.

First-Time Git setup

The commands discussed further would be on Ubuntu as this is my preffered platform for development

Setting up Identity

Go to terminal and type in:

$ git config --global user.name "Your Username"
$ git config --global user.email youremail@examplemail.com

Note:
Please use your username instead of “Your Username”
and your email id instead of youremail@examplemail.com
in the process mentioned above.

If you like to check the settings type in:

$ git config --list

Want help?
Try:

$ git help

The Basics

You don’t necessarily have to be online
Most of the operations that are done in git are done offline; there are very much little things that you can’t do if you are offline.

Everything You do is Marked, Git stores changes that you make in its database by the hash value of the contents.

The Three States

According to Git, a file can be in any of the following three stages

  • committed
  • modified
  • staged.

So what are they ?

Committed: This means that the data you have made is stored in your database.

Modified: This means that you have made changes to the data, but you have not yet committed it.

Staged: This means you have a made a file as modified in the current version of the file and is ready to go to next commit snapshot.

Ok fine so…


How to Get a Git Repository?

There are two ways to get a git repository.

  1. By importing an existing project that you are working on to git.
  2. Cloning an existing Git repository from another server

To import the existing project to git do the following.

$ git init
$ git status
$ git add
$ git commit -m "enter a commit message to refer later"

To clone an Existing Repository.

From GitHub

$ git clone https://github.com/the_remaining_url

From Gitlab

$ git clone https://gitlab.com/the_remaing_url

We can follow this method and change the link accordingly from where you want to clone the repository.

Forking an Existing Repository

Forking a repository is like creating a copy of the repository for yourself so that you can do different experiments on it without actually doing any damage to the original project.

How to fork a repository ?

Go to the repository page which you want to fork
click the fork button
you should get a personal copy of the repository on your host-page

Cloning the forked repository and Setting up upstream

$ git clone https://github.com/YOUR-USERNAME/ProjectName
$ git remote add upstream https://github.com/ORG-NAME/ProjectName

Upstream is added so that you can update your local repository as the Repository of the Organisation Updates.

How to Record changes made on the Repository ?

Most of the time you are working with VCS you would be mainly focusing on the project you are working on and would not remember what stage your files are so to know the status of your file you have to use the following command.

$ git status

To track new files, to stage files or to mark merge-conflicted files as resolved you need to use this command.

$ git add 

Ignoring files

There might be some files that you might be having in your project which you don’t want git to track. To deal with this, you have to create a file named. gitignore

 $ >.gitignore

now you that you have a .gitignore file open it using any text editor

here you can add the names of files/folders that you want to ignore

e.g.:

Java class files
*.class

Java class files
*.class

Generated files
bin/
gen/
build.xml

etc..

To see what you have staged and Unstaged changes use :

$ git diff

How to commit changes ?

There are different ways to do this

Using the interactive window

$ git commit 

and follow the steps on the interactive GUI

using a single terminal command

$ git commit -m "write you commit message here."

Removing files

$ git rm 

or

$ git rm --cached 

Moving files

$ git mv file_from file_to

you can specify the directory locations accordingly to file_from and file_to

How to view the history of your files ?

Here also there are many ways to do it lets look at some of them.

To get a necessary overview use:

$ git log

To get the overview along with the stats on the changed files using:

$ git log --stat

To get a formatted single line overview use:

$ git log --pretty=oneline

Want a graphical overview ?

$ git log --graph

Ok now you have come to the point where you have to start undoing stuff
Let’s do exactly that..

Unstagging a staged file

$ git reset HEAD filename

or

$ git rm --cached filename

Ok, you have made changes to a file and messed it up now you want to undo the modification done to the file use:

$ git checkout -- filename

now that you have made changes if you want to commit these changes use:

$ git commit --amend

Most of the time you might be working on the project of an organisation, and you might need to maintain more than one remote repositories.

Working with remotes

To see your remote use

$ git remote

This will show the remote name

To see remote name with url use:

$ git remote -v

Fetching from remote repositories

When you want to download new data from a remote repository, but you don’t want the changes to be applied to your local repository, you can use fetching.

$ git fetch [remote_name]

Pulling from a remote repository

When you want to download new data and apply the new changes to your remote repository you can use pull, the command for the following is:

$ git pull [remote_name] [branch_name] 

Pushing to a remote repository

When you have made necessary changes on your local repository, and now you have to update the cop of your repository use:

$ git push [remoteName] [branchName]

If you want to remove a remote you can do it simply by using this command:

$ git remote rm [remotename]

And for renaming:

$ git remote rename [current_name] [new_name]

Branching

So you must be having a project now and you want to implement a new feature or fix a bug in that case you don’t want to mess up with the master of your project this master is called a branch of the project just like the branch of a tree so you can have more than one branch.

Creating a branch

$ git branch [branch_name]

How to switch between branch ?

$ git checkout [branch_name] 

Now you can stage and add files in this branch and work on the new branch you have created without breaking the main development project code.

If you want to merge two branches, you can do the following.

$ git merge[the_branch_to_be_merged]

Renaming branches

$ git branch -m new_name

or

$ git branch -m old_name new_name

Tagging on git

While developing software, there would be many versions or release points, and you might want to mark this, which could be accomplished by using tagging.

There are different ways to tag a project.

Lightweight tags

$ git tag

Annotated tags

$ git tag -a {tag name}

When you want to see the details of a tag, you can do this.

$ git show {tag name}

If you wish to List the tags

$ git tag -l

When you are involved in a project for a long time and using git to and forth you might end up in a situation where you are working on a branch but have not committed any changes as you don’t want to apply the have done work but need to switch branch or need to work on something else, in this case, you can save the dirty stage of your project by using something known as staching. The stash can be reapplied at any time so that you can continue working when you wish to.

How to Stash ?

Making a stash

$ git stash

or

$ git stash save "message."

Applying a stash

$ git stash apply stash@{stashIndex}

How to list all your stash’s ?

$ git stash list

Unapplying a stash

$ git stash-unapply

How to squash commits ?

when you are working on a project there might be times where you are making many commits but finally, realise that you need to record all these changes as one commit then you can squash your commits.

$ git rebase -i <from-this-commit

insted of <from-this-commit> you can use either the SHA1 hash or the position from the head

eg. if you want to squash 2 commits from head use

$ git rebase -i HEAD~2

Then you will be taken to an interactive windows where you can pick the top commit and rename the rest to squash, after this you can set the commit message for all the commits.

 

This is just my basic explanation of git which I have learned while collaborating on GitHub if you are more interested in git and you wish to learn more refer

https://git-scm.com/doc

 

 

 

 

Leave a comment

Design a site like this with WordPress.com
Get started