Tech Basics
Git Tricks
Git | GitHub | SCM
--
Objective
This post talks about few tips and tricks using Git in daily usage. Nothing new or fancy in this post, just a standard stuff which I found helpful in my day to day work. I made an assumption for this post that you are already have familiarity using Git. Some of content, I have taken from Internet search.
Git Pull and Fetch
When you clone someone’s repo to start work, you are making a copy of it. After that you may have to get the changes sometimes, which have been applied to the original source. The Pull
and Fetch
are your tools.
git fetch
is the command that tells your local git to retrieve the latest meta-data info from the origin. So it updates your remote-tracking branches under refs/remotes/<remote>/
. This operation is safe to run at any time as it never changes any of your local branches under refs/heads
.
git pull
is the command that tells your local git branch to retrieve the latest changes from the remote repository, while also updating your other remote-tracking branches.
In other tech words 😵
git fetch
fetches updates but does not merge them.
git pull
does a git fetch
under the hood and then a merge
.
Some people prefer to use a trick before pushing changes to remote.
git pull --rebase
It will automatically sync latest server changes (git fetch
+ git merge
) and will place your commit at the top in git log. No need to worry about manual pull/merge.
Deleting Branches Local & Remote
If you want to delete a branch in local, use following -
git branch -d <branch_name>
git branch -D <branch_name>. # Force-delete un-merged branches
If you want to delete a remote branch -
# to delete only local remote-tracking branchgit push <remote_name> --delete <branch_name>
# <remote_name> is origin most of the time# To delete that actual remote branchgit branch --delete --remotes <remote>/<branch>