Git tutorial – Getting and Creating Projects

What is Git?

Git is a version control system like SVN, CVS etc.

Git originates from the Linux development and now it is used by most popular Open Source projects, e.g. the Eclipse developer teams, as well as many commercial and public organizations.

The core of Git was originally written in the programming language C, but Git has also been re-implemented in other languages, e.g. Java, Ruby etc.
You can introduce yourself to Git with your name and public email address before doing any operation. The easiest way to do so is:

Importing a new project into Git repository

Assume you have a existing project with your initial work. You can place it under Git revision control as follows.

you will get message like below –

Initialized empty Git repository in .git/

You’ve now initialized the working directory–you may notice a new directory created, named “.git”.

Next, tell Git to take a snapshot of the contents of all files under the current directory (note the .) or *, with git add:

This snapshot is now stored in a temporary staging area which Git calls the “index”. You can permanently store the contents of the index in the repository with git commit:

This will prompt you for a commit message.

List available branches
The git branch command lists all local branches. The currently active branch is marked with *.

Making changes

Modify some files, then add their updated contents to the index:

You are now ready to commit. You can see what is about to be committed using git diff with the –cached option:

(Without –cached, git diff will show you any changes that you’ve made but not yet added to the index.)

use “git reset HEAD …” to unstage

Finally, commit your changes with:

Alternatively, instead of running git add beforehand, you can use, m is the commit messages generally this is the reason for which you commited the code

Viewing project history

At any point you can view the history of your changes using

If you also want to see complete diffs at each step, use

Creating a new branch
To create a new branch named “experimental”, use

If you now run

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

Switching Branch

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

You can make a different change on the master branch:

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

If there is any conflicts run the below command;

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

will commit the result of the merge. Finally,

Deleting a Branch

At this point you could delete the experimental branch with

Cloning Remote repo using Git

git clone -b <remote_repo>

Suppose Git repository in location is , if some one wants to contribute, can use below command

This creates a new directory with branch name containing the clone of the above repository.

Pull code from Repo

git fetch is that the following will tell you any changes in the remote branch since your last pull.

Only fetches from the remote branch but it does not merge

Pulls from a remote branch and merges it.

Saves stuff from your current branch that isn’t in the upstream branch to a temporary area. Your branch is now the same as before you started your changes.

Git history

Git history is represented as a series of interrelated commits.

merge-base: Clarify the comments on post processing.

We can give this name to git show to see the details about this commit.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *