Git Commands 2

Branching 


Every project by default has a branch and it is called the master branch. You can even create more branches on your need.
Branching allows you to create a separate copy of your working directory/master branch and do variations in the code without changing the working directory/master branch.

Think of an instance where you have to contribute to a project where multiple developers are being involved. The best practice is to clone the project into your PC, create a separate branch for yourself and then start developing.

A developer can have/create many branches of the same working directory, where he intends to try out many options. And remember each branch is independent of each other.

git branch           
Lists all the branches in your repo, also it indicates the current branch you are in with a star mark.

git branch branchName                
Creates a brandnew branch

git checkout nameOfTheSecondBranch
Switch between branches, once this command is run you’ll be switched from your current branch to the nameOfTheSecondBranch branch
When using Git Bash to switch between branches, it is wise to do a refresh in the IDE, following every checkout command.

git push repoName branchName       
Pushes a branch to the remote repository


Merging


This is the reverse of branching and considered as a key feature of Git.

Whenever you have 2 branches in your PC that needed to be merged together, you can use the following command,
git merge nameOfTheBranchYouWantMergeTo

eg: git merge master    
➤so now your current branch is merged with the master branch and your current branch is updated to the merged version

If the content in two files with same name differs among the two branches, there occurs merging conflicts, and Git leaves you to resolve these, so you have to go through the code and continue merging the code.


Clonning


When you have been made a contributor to a project, but still you still don’t have the code to begin with, you have to import the code to your PC, before start developing.
This has been made easy with the git clone command.

You simply have to copy the SSH link of the relevant repo.
Go to the location where you wish to save the project code, open Git Bash and run the following command
git clone SSHLink
Enter your password when asked, and once the process is finished you’ll see a cloned project in your PC
Make it a practice to create a branch for yourself before start coding!😊


PULL

This is very similar to merging, except that this time it synchronizes your local repo with the remote repo.

Think of an instance where you’ve cloned a project and working in your own branch and later some team member notifies you about  a change in the original master branch (the original master branch is not in your PC)
Now, the master branch in your PC does not contain the latest changes, to synchronize the master branch in your PC to the most current master branch you need to do a PULL😊
               
git pull nameOfTheRepo branchYouNeedToPullFrom


**When using Git, it is recommended to do a PULL before every PUSH, so that it guarantees to have the latest version of the code and also it minimizes the conflicts when you do a PUSH.





No comments:

Post a Comment