Saturday 14 November 2015

Git Chit #1

Hola  Amigos!!

Diwali Greetings ! :)

So, this Diwali I had a few explosions with GIT,a tool budding developers hate as much as the pro developers adore.

[ LOL just check out the meaning of git :P It says "unpleasant or contemptible person". This will sooth you out if you have stumbled upon this post in frustration,desperately searching for your git solution. ]

So for those who are new to GIT, I must say it is a version control system, more here
In layman language, we all know is a complicated process to write and maintain code of projects especially when large number of people are involved, so git helps a lot. You can create branches, work in parallel on different features, undo the changes and do several other tasks involved in a simplified manner.




In this post we would be discussing a some use cases, i.e. what to do when a particular thing happens (based on my past experiences).

USE CASE #0
Usually when you are working on forked repo, you wish to get the changes done on the original repo too.You can easily do this by adding upstream.
git remote add upstream <git link of repo>

Now before doing any changes on local branch, do this:
1. git fetch upstream
2. git checkout master
3. git merge upstream/master

USE CASE #1
Deleting a file: (Do not do Shft+Del or Del directory. This will create problems.)
git rm <filepath>
git commit -m "Commit Message"

USE CASE #2
When you have done several not so useful commits, it becomes hard to follow in a PR, you can use git rebase. Eg for last five commits:  "git rebase -i HEAD~5" and squash them together and than git push --force ...
Have a look : https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase-i

USE CASE #3
When working on different issues or features it is good to work on separate branches and create a separate PR from each one of them. This can be easily done by creating a new branch by using git branch -b <branch name>and checkout that, now work on that.
If you initially had just one branch and now wish to create a Pull Request with selected commits, use git cherry-pick feature, it's awesome!
So basically, do git log, it will show you all the commits. Now select the hash value of commit from which you which to create a new branch, so do :

You can create the branch via hash,
git branch branchname <sha1-of-commit>
or by using a symbolic ref.
git branch branchname HEAD~3
Now checkout to this branch,to include specific commits in it, use:
git cherry-pick <sha1-of-commit>

Do git log to get an idea of commits included in this branch.
Now do git push origin <branchname>
This will create the required PR.

So this was the first git chit.

Hope that helps!

Will see you in next post, hopefully soon, till then, keep hacking! :)


A secret love message ~

Hmm, so the trick of putting up a catchy seasonal title worked out, we got you here! Folks, we will be talking about a really cool tech...