Featured Post

Python Virtual Environments

GIT PULL REQUEST is confusing at first, then it makes sense!

 I have been dabbling with Git since last week and I thought I should write down whatever I have learnt. Earlier I had written an article about HOW TO GIT, this is part two of it. I will be mostly covering how developers mostly use Git and how it works in real world and what is a pull request?! As you know Git is a version control software. But how exactly do you start using it? and what you need to do. If you are a GIT NOOB like me, read on .. 

First of all you need to install git on your machine. That is a prerequisite. Then there are two ways of working with files. Either you have created few files on your laptop and kept it in a folder or you need to download the project files into your folder from git hub. Now, downloading the project files from github is called cloning. To clone, you need to right click in the folder and hit open git bash here. then  you run the command `git clone <url of repository> <your folder name>`  from git bash. And that will download all the files from that repository into you folder. 

What is a repository? It is nothing but a folder system containing all the project related details, folders, files and  branches. Branches? It is similar to branches of a tree. Now by default what ever files you have will be in the master branch. This is the main branch. We will come back to this master branch in a bit..

 Lets say you have a folder with few files in them that you are working on in your laptop and you need to do some version controlling on them. Open git bash in that folder. Then you run command  `git init` This will initialize the folder. Meaning, you tell git bash that - "bhai dekh lo, apna folder h! sambhal k" also you need to check if you have named your machine. git config --global user.name yourname and git config --global user.email youremail. Git config --global list is used to check the configs.  Now when you add the files to the folder in your laptop, these are in untracked condition, means git doesn't know what you are doing in those files. you need to add them to staging area. `git add -A` will add everything to stage. From here on whatever changes you do, git will make note of it.  From there you run `git commit -m "comment"` this will commit the files. This is more or less like "saved". Lets say you need to do some more changes but not sure how it may react. so you can create a new branch say git checkout -b <branchname> and all files will be available in the new branch which is in master. Now you can do any changes in the new branch and not worry about anything. If you wanna go back to master, `git checkout master`. 

So far what ever you did was in local computer local repository. Now what if you would like to save it in git hub. the online drive . Here it is called remote repository. then you need to set the repository URL to your origin. git remote set-url origin https://url.git       Once the url is set, make some token settings. then `git push origin master` this will push whatever changes you have made in the master to the master in the remote repository. this is where the fun begins! 

Now, If there is some changes in the master(remote) already which is not in your master(local) then when you are trying to push, the git will throw error saying changes are there. Here you need to pull the master(remote) into your local then apply your changes to it and then you can push it to master. whereas, if you have a  branch, in remote and local, then you can push your local branch to remote branch and it will work flawlessly. 

Say, now you have made changes to branch in local. then you have pushed the branch to remote. Now you would like to merge the changes in remote branch with the remote master. but here you need to take permission from the owner of the master. lets call him master guy.  this is because if a lot of ppl are working and simpsimply updating things to master, it will get wacked . so the master guy now has to check what changes you have made and then approve it to be merged. But if he has to see your code, he needs to pull the code from your remote branch, inspect it and then merge it with master. This process is called pull request. Where the branch guy pushes code to remote branch and raises a pull request to the master guy. the master guy then pulls the code onto his machine and inspects and then he will pull the code into the master to merge. Hence the name pull request.

No comments:

Post a Comment