Git
As web
developers, a lot of times we work/develop application in local system then
just upload everything to server when we are done. This is fine when single
developer or doing small changes in the code, but when we are dealing with more
than one person working on something or large project. That’s simply not
feasible. So, we are turning to Version Control.
This
article main focusing on Git basic command to setup and handle the code in Git
Version Control.
What is Version Control System?
A version control system allows you to track the history of a collection of files and includes the functionality to revert the collection of files to another version. The collection of files is usually source code for a programming language but a typical version control system can put any type of file under version control.
A version control system allows you to track the history of a collection of files and includes the functionality to revert the collection of files to another version. The collection of files is usually source code for a programming language but a typical version control system can put any type of file under version control.
What is Git?
Git is a
free and open source distributed version control system. A distributed version
control system does not necessarily have a central server which stores the
data.
The user can copy an existing repository. This copying process is typically called cloning in a distributed version control system. Typically there is a central server for keeping a repository but each cloned repository is a full copy of this repository in your local system.
The user can copy an existing repository. This copying process is typically called cloning in a distributed version control system. Typically there is a central server for keeping a repository but each cloned repository is a full copy of this repository in your local system.
Git vs. SVN
Here are
going to discuss the few of the difference between SVN and Git version control
system.
1)
Git is distributed Version Control System
2)
You can save/commit your local file change and
create new branch without have network connection.
3)
Git is stored the changes like content as
metadata, svn stored just files
4)
SVN creates .svn directories in every single
folder (Git only creates one .git directory)
5)
You have to tell SVN whenever you move or delete
something. Git will just figure it out.
6)
Ignore semantics are easier in Git. If you
ignore a pattern (such as *.pyc), it will be ignored for all subdirectories.
(But if you really want to ignore something for just one directory, you can).
With SVN, it seems that there is no easy way to ignore a pattern across all
subdirectories
Git Terminology
The following table provides a major Git terminology
The following table provides a major Git terminology
Term
|
Description
|
Clone
|
Clone the existing repository in git
|
Add
|
When add your new/updated file to be added to staging area
|
Commit
|
When you commit your changes files (Added files) in your local
repository
|
Push
|
Push will move the committed, list of files to the remote git
repository
|
Pull
|
Pull updated the others committed files in your local.
|
Branches
|
A branch is a named pointer to a commit. Selecting a branch in Git
terminology is called to checkout a branch.
|
Tags
|
A tag points to a commit which uniquely identifies a version of the Git
repository. With a tag, you can have a named point to which you can always
revert to. You can revert to any point in a Git repository, but tags make it
easier. The benefit of tags is to mark the repository for a specific reason,
e.g., with a release.
Branches and tags are named pointers; the difference is that branches
move when a new commit is created while tags always point to the same commit.
|
Git Global Configuration
Before
go to start the creating new repository or project in git. We need to set the
global variable configuration (.gitconfig). It will author helpful to tracking
the history of the file changes in git.
In each Git repository you can
also configure the settings for this repository. Global configuration is done
if you include the --global flag, otherwise your configuration is specific for
the current Git repository.
To setup the configuration use the git config command
Git Global Configuration
Before
go to start the creating new repository or project in git. We need to set the
global variable configuration (.gitconfig). It will author helpful to tracking
the history of the file changes in git.
In each Git repository you can also configure the settings for this repository. Global configuration is done if you include the --global flag, otherwise your configuration is specific for the current Git repository.
To setup the configuration use the git config command
# configure the user which will using this git
git config --global user.name "your name"
# user email address for using this git
git config --global user.email "your.email@gmail.com"
# proxy configuration setting
git config --global http.proxy http://<corpusername>:<corppassword>@<proxyaddress>
Instead of two step create and checkout to the branch. You can directly create a branch and checkout to that branch using “git checkout –a” command
Delete
a Branch
In each Git repository you can also configure the settings for this repository. Global configuration is done if you include the --global flag, otherwise your configuration is specific for the current Git repository.
To setup the configuration use the git config command
# configure the user which will using this git
git config --global user.name "your name"
# user email address for using this git
git config --global user.email "your.email@gmail.com"
# proxy configuration setting
git config --global http.proxy http://<corpusername>:<corppassword>@<proxyaddress>
Cloning remote repositories
You
can use the git clone command to clone existing repositories in git (Github or
Gitlab).
# clone the remote
repositories (Master) in local system
git clone <HTTP/HTTPS/SSH Clone Url >
git clone <HTTP/HTTPS/SSH Clone Url >
# clone the particular
branch from git repository git clone <HTTP/HTTPS/SSH Clone Url > -b <branch-name>
--single-branch
Every
clone contains the full history of the files and a cloned repository has the
same functionality as the original repository.
Git
Status
Git
status command is help to show current status of the current branch. What are
the files are changed, new added, conflicts and removed.
# show the status of
the current branch
git status
git status
Adding
changes to your Git Repository
You
add changes in the working tree to the staging area with using git add command.
The git add command allows you to incrementally modify files and stage them.
Until you are complete your tasks.
# Staging all the
modified files
git add -A
git add -A
# if you need to
staging only the particular files. Add multiple files with space
git add <filepath/filename><space><filepath/filename>
git add <filepath/filename><space><filepath/filename>
# Here we are added
test.php and test1.py files to staging
git add testfolder/test.php testfolder2/test1.py
git add testfolder/test.php testfolder2/test1.py
Committing
to the repository
After
adding selected file to the staging, you can commit those files permanently to
the git repository. Committing create new persistent snapshot called commit or
commit object.
# Commit the files
with short message
git commit –m “Type the short message to under that commit purpose”
git commit –m “Type the short message to under that commit purpose”
Git Pull
The get pull command will help to get the latest updated
from other people of the branch.
# pull the updated
code from remote
git pull origin <branch name>
git pull origin <branch name>
When pulling the updated code from origin (Remote
Repository), if someone worked on the same file. You may be end up with the conflict file. In
that case you need to solved the conflict file and push to the remote
repository.
Git Push
Git
push command is use to publishing your local changes (local commit) to the
remote repository.
# pushing the local
commit files to remote
git push origin <branch name>
git push origin <branch name>
Git Branch
You
can create new branch and work on independently from each other. Default branch
most often called master.
You
can create branch regular basis. Whenever working on new
functionality/features.
# list all the local
branch names
git branch
git branch
#Output Current
branch name with highlighted with (*)
* develop
feature-optimization
feature-rights-integration
feature-usergroup
master
# listing all the local and remote branch names
git branch -a
* develop
feature-optimization
feature-rights-integration
feature-usergroup
master
# listing all the local and remote branch names
git branch -a
# listing all the
remote branches names
git branch -r
git branch -r
The
–v option list more information about the local branch (Display with last
commit message and commit id)
Create Branch
Create
a new branch using git branch <branch name> command. This command allows
specifying the starting point (commit id, tag, remote or local branch). If not
specified the commit to which the HEAD reference points is used to create the
branch.
# create new branch
git branch <branch name>
Your local git repository contains references to the
branches on the remote repositories to which it is connected. This is call
remote tracking branches.
Create
new tracking branches by specifying the remote branch during the creation of a
branch.
# create a tracking
branch called ‘testbranch’
# which tracks origin/testbranch
# which tracks origin/testbranch
git
branch testbranch origin/testbranch
To
see the tracking branches of the remote repository use the below command
# show all remote and
tracking branches for origin
git remote show origin
git remote show origin
Checkout Branch
To
start working in a branch first you have to ‘checkout’ the branch. If you checkout a branch, the HEAD pointer
moves to the last commit in that branch.
# Switch to your new
branch ‘testbranch’
git checkout testbranch
git checkout testbranch
Instead of two step create and checkout to the branch. You can directly create a branch and checkout to that branch using “git checkout –a” command
# create branch and
switch to it
git checkout –b <branch name>
git checkout –b <branch name>
Rename
a Branch
# rename a branch
git branch –m <old branch name> <new
branch name>
To
delete any branch, first you need to make sure you are not in that branch
currently and that branch should not have any uncommitted changes.
# delete a local
branch
git branch –d <branch name>
#Force to delete the branch
git branch –d <branch name>
#Force to delete the branch
git branch –D <branch name>
Delete the branch in a remote
repository using the following command,
# Delete the branch in
remote repository
git push origin : <branch name>
#Alternative command
git push origin : <branch name>
#Alternative command
git push origin - - delete :<branch name>
Differences
between branches
# Differences between
two branches
git diff <branch name1> <branch
name2>
Git
Merge
Git Merge allows you to combine the change of the two
branches. Git Merge command is merge the branch to your current checkout branch
only.
# Merges into your
currently checked out branch
git merge <branch name>
#Eg. I need to merge develop branch to master branch
#Step 1 checkout to master branch
git checkout master
#Step 2 Merge develop branch
git merge develop
git merge <branch name>
#Eg. I need to merge develop branch to master branch
#Step 1 checkout to master branch
git checkout master
#Step 2 Merge develop branch
git merge develop
Git Ignore
Git has
provided the option to add the files and directories to ignore list. The
.gitignore file tells Git to ignore the specified files in Git commands. You
can still add ignored files to the staging area of the Git repository by using
the --force parameter, i.e. with the git add --force [filename] command.
This is
useful if you can add the auto-generating log files, local server database,
proxy and mail server configuration files.
You can also revert back those files whenever you need, using “—no-assume-unchanged”
You can also revert back those files whenever you need, using “—no-assume-unchanged”
# add files to ignore
list
git update-index –assume-unchanged <files list>
#Eg. I need to add database.php and error_log.txt in Ignore list
git update-index –assume-unchanged database.php error_log.txt
git update-index –assume-unchanged <files list>
#Eg. I need to add database.php and error_log.txt in Ignore list
git update-index –assume-unchanged database.php error_log.txt
# Revert back the
ignore list files
git update-index --no-assume-unchanged <files list>
git update-index --no-assume-unchanged <files list>
Git
log
The
git log command shows the history of your repository in the current branch,
i.e. the list of commits.
# show the history of commits
in the current branch
git log
git log
# filter the log output by a certain author
git log --author=<authorename>
git log --author=<authorename>
Tags in Git
Git has the
option to tag a commit in the repository history so that you find it easier at
a later point in time. Most commonly, this is used to tag a certain version
which has been released.
If you tag a commit, you create
an annotated or lightweight tag. Git supports two different types
of tags, lightweight and annotated tags.
A lightweight tag is a pointer to
a commit, without any additional information about the tag.
An annotated tag contains
additional information about the tag, e.g. the name and email of the person who
created the tag, a tagging message and the date of the tagging.
Annotated tags can also be signed and verified
with GNU Privacy Guard (GPG).
# List the available tags
git tag
# Create the lightweight tags
git tag <tagname>
#Create annotated tags
git tag <tagename> -m “<tag note>”
#Create tags for a certain commit id
git tag <tagename> -m “<tag note>” <commit id>
# Create the lightweight tags
git tag <tagname>
#Create annotated tags
git tag <tagename> -m “<tag note>”
#Create tags for a certain commit id
git tag <tagename> -m “<tag note>” <commit id>
Thanks You Guys :-)
No comments:
Post a Comment