Tuesday, December 19, 2017

A Step by Step Guide for Learning Git without Pain

Git: Best Version Control System(VCS)
Why we use git?
  • It can track changes in your files.
  • It simplifies working on files and projects with multiple people(developers).
  • It can help to get involved in open source.
Who are giving support and free repository service to keep your project?

Install Git:
Windows: Download from [here] 
Linux/Mac: sudo apt-get install git

Git Configuration:
Run the following command:
$ git config -global user.name “Your Name”
$ git config -global user.email you@example.com
Here, 
  • Your Name will be replaced by your name.
  • you@example.com will be replaced by your email address.
 -global It will set user.email and user.name permanently. So you won’t have to configure it again next time if you are committing another project.

How to initialize a git repository for a project?
Go to your project directory. Then run the following command:
$ git init
Initialized empty Git repository in /Users/zakir.rizvi/newProject/.git/
It will create .git folder[hidden]. It keeps track all of your project files.
$ git add HelloWorld.java
This command will add HelloWorld.java file to git. It registers this file and starts keeping track of this file.
If we want to add all files to our project directory, run the following command:
$ git add .
Then run the following command to make a commit.
$ git commit -m ‘Initial Project’
Then you can push your code to master repository. It will put your code on the remote server.
$ git push origin master
It will ask for username and password. You have to provide those.
===========================

Various git commands in a single snapshot:


Create a branch:
[TO CREATE A NEW BRANCH]:
$ git branch myNewBranch
Checkout branch:
[MOVE FROM ONE BRANCH TO ANOTHER]:
$ git checkout myNewBranchName
[TO CREATE A NEW BRANCH AND MOVE TO THE NEWLY CREATED BRANCH]:
$ git checkout -b  myNewBranchName 
Delete branch:
[TO DELETE A BRANCH]: 
[FIRST CHECKOUT TO A DIFFERENT BRANCH THEN DO]:
$ git branch -d branchName
[IT WILL ONLY DELETE THE BRANCH LOCALLY BUT IF YOU WANT TO DELETE IT REMOTELY]
Then do:
$ git push -d origin branchName
===========================
How do you go back to your previous commit? 
$ git reflog
It will show the list of previous commit like below:
$ git reflog
1769285 (HEAD -> loc-dp-fibonacci-201712, origin/loc-dp-fibonacci-201712) HEAD@{0}: commit: Dyanamic Programming(Fibonacci)
1e0c543 (br#1014) HEAD@{1}: checkout: moving from br#1014 to loc-dp-fibonacci-201712
1e0c543 (br#1014) HEAD@{2}: checkout: moving from br#1013 to br#1014

Now you want to come back to previous commit id 1e0c543.
$ git reset --hard 1e0c543
So, it will make your head to 1e0c543 commit id.
========================
Why merge conflict occurs?
Ans: When multiple people work in the same file, then merge conflict occurs. Because the changes are not synchronized properly.

How to resolve to merge conflict?
Ans:
You can do it by two ways:
  • fix it manually
  • fix by mergetool

Conflict Zone in a file looks like below:
int currentPrice = 1100;
<<<<<<< HEAD
float commision = 12;
=======
float previousPrice = 1150;
int commision = 12;
>>>>>>> branch-name

Meaning of all signatures:
1. <<<<<HEAD
It tells you where your code begins
2. ======= 
It is a marker that tells you that the following part is done(edited and changed) by another person. And it is mismatching.
3. >>>>>>> branch-name 
It is the name of the branch

Resolution Procedure:
To resolve, just compare the two changes.
From the above code snippet, the person having these conflict changed int to float, but the other person added the previousPrice variable, so take out:
int commision = 13; 
from the code and remove all the <<<<<HEAD =======
>>>>>>>branch-name
That’s all. Then commit your code and push. Now everything should be OK.

This merge conflict can also be solved by mergetool also. KDiff3 is a good mergetool. You can download and install from [here]. Configuration procedure is given at the last part. You can check.

After configuration kdiff3, you can run the following command:
$ git mergetool
If youhave multiple mergetool or is not configured properly, then run the follwing command
$ git mergetool --tool=kdiff3
========================
How to rename the author in git?
$ git config -global user.name “Your Name”
$ git config -global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
$ git commit -amend -reset-author
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index.js
========================
Installation procedure of KDiff3: 

For windows users:
Download from https://sourceforge.net/projects/kdiff3/ and then install
git config --global --add merge.tool kdiff3
git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global --add mergetool.kdiff3.trustExitCode false
git config --global --add diff.guitool kdiff3
git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global --add difftool.kdiff3.trustExitCode false

For Ubuntu/Linux users:
Installation:
sudo apt-get update
sudo apt-get install kdiff3
Then configure:
git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.cmd "/usr/bin/kdiff3 --merge --result=\$MERGED \$LOCAL \$BASE \$REMOTE"
git config --global mergetool.keepBackup false
git config --global diff.tool kdiff3
git config --global difftool.kdiff3.cmd "/usr/bin/kdiff3 \$LOCAL \$REMOTE"

For MacOS users:
Installation:
sudo apt-get update
sudo apt-get install kdiff3
Then configure,
git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.cmd "/Applications/kdiff3.app/Contents/MacOS/kdiff3 --merge --result=\$MERGED \$LOCAL \$BASE \$REMOTE"
git config --global mergetool.keepBackup false
git config --global diff.tool kdiff3
git config --global difftool.kdiff3.cmd "/Applications/kdiff3.app/Contents/MacOS/kdiff3 \$LOCAL \$REMOTE"

Hope it will help to learn git.

No comments:

Post a Comment