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>
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 >
# 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
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
# if you need to
staging only the particular files. Add multiple files with space
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
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 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>
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 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
#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
# listing all the
remote branches names
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
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
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
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>
Rename
a Branch
# rename a branch
git branch –m <old branch name> <new
branch name>
Delete
a Branch
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>
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 - - 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 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”
# 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
# Revert back the
ignore list files
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
# filter the log output by a certain author
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>
Thanks You Guys :-)