Pages

Monday, December 28, 2015

Advanced Git

We have seen the basics of Git and how to create a basic repository and commit files. In this next article we will see the advanced topics of git.

Git Configuration – In order to communicate with the remote repository from our local repository we need to configure user name and email so that git repository will identify who has done operations on the repository. In order to configure those use the below commands as,

git config --global user.name <user Name>
git config --global user.email <Email ID>

Remote Repository Details – Though we have the local repository we need to have a remote repository where we can push our changes so that other can access them. In order to do this, we need to first add the remote Repository URL. This can be done by using

git config remote.origin.url https://github.com/abc/abc.git

Once the remote repository is set, we can get the details using

[root@vx111a master]# git config remote.origin.url
https://github.com/abc/abc.git

Configuration Details
In order to get the configuration details like user.name and email etc, we can get those details using

[root@vx111a master]# git config -l
user.name=jagadish12
user.email=jagadesh.manchala@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/abc/abc.git

Remote Location Details
In order to view the remote location details we can use,

[root@vx111a Mytest]# git remote -v
origin  https://github.com/jagadish12/MyTest.git (fetch)
origin  https://github.com/jagadish12/MyTest.git (push)

[root@vx111a Mytest]# git ls-remote --get-url origin

Clone a Repository
Cloning is nothing but a way to access a repository. Checking-out in SVN is similar to Cloning in GIT. Running git clone <repository URL> will pull in a complete copy of the remote repository to your local system. Now you can work away on it, making changes, staging them, committing them, and pushing the changes back. We can clone using

[root@vx111a test]# git clone https://github.com/jagadish12/SampleTest.git
Cloning into 'SampleTest'...
remote: Counting objects: 24, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 24 (delta 1), reused 24 (delta 1), pack-reused 0
Unpacking objects: 100% (24/24), done.

Branching
We need to make sure that our master repository is always clean. Clean in the sense with out any changes. For the changes that we are doing before moving them to the master we need to make sure that we create a branch of the code and make the changes to the branch. Creating branches in git is done using

[root@vx111a]# git checkout -b test
Switched to a new branch 'test'

Check the Branch Status -
[root@vx111a]# git status
# On branch test
nothing to commit, working directory clean

Check the available branches -
[root@vx111a]# git branch
  master
* test

Delete the whole branch using
[root@vx111a]# git checkout master
[root@vx111a]# git branch -D test

For deleting we need to first move out of branch and then we need to delete that branch

When you do a pull request on a branch, you can continue to work on another branch and make another pull request on this other branch.

Merging
Once your feature is finished, you’ll want to merge your branch back to the master branch. This is way easier than it would be in Subversion. You just execute:

    git checkout master
    git merge <feature_branch>

If there are conflicts, you will be prompted to fix them. Add the conflict fixes to the changeset with git add, and then commit.

Git add and Commit
Once the changes are done, we need to first make sure the changes are added to the local repository and then once added need to commit to the remote repository

For adding the file use, git add <file Name> or git add . ( all files)

For committing the file, we need to use the command,
git commit –m “Message for commit”
If we want to commit only specific files we can use
Git commit file1 file2

Undo Git Commit
There are cases where we need to revert back the commits done. Git provides us the ways using

git reset <file> - which will remove it from the current index (the "about to be committed" list) without changing anything else.

Or we can use “git reset” without any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.

Reset
Consider that we called git add -all, now all files are added. I did not make a commit and push. How can i undo my action?

Git provides us a way for undoing the action by using “git reset HEAD”

Push to origin
Once the changes are committed to the local repository, we need to make sure the changes are pushed to the remote for other developers to access. This can be done using

[root@vx111a SampleTest]# git push origin master
Username for 'https://github.com': jagadish12
Password for 'https://jagadish12@github.com':
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/jagadish12/SampleTest.git
 * [new branch]      test -> test

This will push the local repository changes to the remote repository. master is the local repository.

Git Logging
As other SVN tools, Git supports logging. We can use commands for getting the basic details like,

[root@vx111a SampleTest]# git log
commit 3b65aa9e103740a323ee157b93c6f5a4785e4f25
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Fri Dec 4 13:44:51 2015 +0530

    Hello World Index Changed

commit c384865e37e6848a847859b94a1d7e50d61f2c31
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 19:54:53 2015 +0530

    second Check

commit b96af507d8e407e6c99b8a1e6c6ef5f61152657c
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 19:52:39 2015 +0530

    Sample Test Checking

The “git log” command gives us the basic details of the recent commits and who has done that.

The logging can be minimized using ,
 [root@vx111a SampleTest]# git log --oneline
3b65aa9 Hello World Index Changed
c384865 second Check
b96af50 Sample Test Checking

Pretty logging is also available as,

[root@vx111a SampleTest]# git log --pretty
commit 3b65aa9e103740a323ee157b93c6f5a4785e4f25
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Fri Dec 4 13:44:51 2015 +0530

    Hello World Index Changed

commit c384865e37e6848a847859b94a1d7e50d61f2c31
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 19:54:53 2015 +0530

    second Check

commit b96af507d8e407e6c99b8a1e6c6ef5f61152657c
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 19:52:39 2015 +0530

    Sample Test Checking

Some more logging command include,
git log --graph --oneline --decorate --all
git log --name-status

Remove Remote Branches
Git also provides us ways to remove Branches in the Remote repository. This can be done using

git push origin --delete <branchName>

Git History
Git provides us various commands in working with the History. We can use the basic command like
[root@vx111a git-test]# git log --author=jagadish12
commit c0a6f2e99ae634af6f8d13d6036fa751f9e558c7
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 19:41:37 2015 +0530

    checking

commit 1419b2dce7162f8ef8bda8f9cace0e60beb70c4a
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 18:48:46 2015 +0530

    hai Comitted

commit 00904bbce573da14764b28c9e0f527e4d00833d1
Author: jagadish12 <jagadesh.manchala@gmail.com>
Date:   Thu Dec 3 17:48:52 2015 +0530

    first Check

You can also easily match on multiple authors as regex is the underlying mechanism for this filter. So to list commits by Jonathan or Adam, you can do this:
git log --author="\(Adam\)\|\(Jon\)"

Differences
Git also provides ways for identifying the differences between multiple branches using
git diff <source_branch> <target_branch>

Tag
it's recommended to create tags for software releases.This is a known concept, which also exists in SVN. You can create a new tag named 1.0.0by executing
git tag 1.0.0 1b2e1d63ff
the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag.


By this we had discussed the advanced topics of Git. More to Come 

No comments :

Post a Comment