Git is a distributed version control system. It is working similar like Subversion(SVN). Its core is written in the programming language C. Creation and popularity of Git is closely related to development of Linux. At first it was used to for Linux kernel development, but now it’s used in the many other open source projects.

Git is batter then SVN. SVN’s way of handling repositories is bad, everyone pushes to the same master “repository”. But in Git everyone has their own repositories. Every local copy contains the full history of the collection of files and a cloned repository has the same functionality as the original repository.

Setting Up a Git Repository: To Setup Git some commands are necessary. Open a command shell for the following operations:

Create Directory: The following commands used to create an empty directory which will use as Git repository:-
# switch to home
cd ~/
# create a directory and switch into it
mkdir ~/repo01
cd repo01
# create a new directory
mkdir datafiles

Every Git repository is stored in the .git folder of the directory in which the Git repository has been created. This directory contains the complete history of the repository. The .git/config file contains the local configuration for the repository.

git init – The git init command creates a new Git repository.
$ git init
Initialized empty Git repository in /opt/solvetest/.git/
Where solvetest is the directory we created
Or
git init

git clone – This command copies an existing Git repository.
Cloning automatically creates a remote connection called origin pointing back to the original repository. This makes it very easy to interact with a central repository.
git clone

git add – The git add command adds a change in the working directory to the staging area. This command creates a snapshot of the affected files, if you afterwards change one of the files before committing, you need to add it again to the index to commit the new changes.
git add

git commit – After adding the files to the Git index, you can commit them to the Git repository. The git commit command commits the staged snapshot to the project history.
# commit your file to the local repository
git commit -m “Initial commit”
After you’ve entered a message, save the file and close the editor to create the actual commit.

git log – The git log command displays the state of the working directory and the staging area.
# show the Git log for the change
git log
git log –stat
It display high level overview of the project history.

There are some more important commands as follows:-
Sharing and Updating Projects commands
git push
# to push use the command:
# git push [target]
# default for [target] is origin
git push origin

git remote – You can manage your remote repositories with git remote.
$ git remote
origin
$ git remote -v
origin git@github.com:github/git-reference.git (fetch)
origin git@github.com:github/git-reference.git (push)

git remote add
$ git remote
$ git remote add github git@github.com:solvetest/solve.git
$ git remote -v
github git@github.com:solvetest/solve.git (fetch)
github git@github.com:solvetest/solve.git (push)

git remote rm
$ git remote rm origin
$ git remote -v
github git@github.com:solvetest/solve.git (fetch)
github git@github.com:solvetest/solve.git (push)

git fetch – download new branches and data from a remote repository
$ git fetch github

git pull – fetch from a remote repo and try to merge into the current branch.
git pull ../remote-repository.git/

Branches: – Branching in Git is one of its great feature.
git branch –
# lists available branches
git branch

# lists all branches including the remote branches
git branch –a

Create new branch – You can create a new branch via the following command.
git branch [newname]

Rename a branch
# rename branch
git branch -m [old_name] [new_name]

Delete a branch
# delete branch testing
git branch -d testing

Push a branch to remote repository
# Push testing branch to remote repository
git push origin testing
# Switch to the testing branch
git checkout testing

Differences between branches
# shows the differences between
# current head of master and your_branch
git diff master your_branch

Git marks the conflict in the affected file as follows:
<<<<<<< HEAD Change in the first repository ======= Change in the second repository >>>>>>> solve56692f5ebfd10d8a9ca1911c8b08127c85f8

Leave a Reply