This is Just another post on Git. Nothing new, everything is available in web! I have just collected few important concepts for day to day work.
Remote repositories are versions of your project that are hosted on the Internet or network somewhere.
# origin - default name for remote server
git remote add origin <remote url>
# list remote urls
git remote -v
To track any remote branch
git checkout --track origin/feature
receive changes from remote repo -
git fetch - download changes and then you can evaluate
git pull - combination of fetch and merge
diff command is pretty useful while working with git.
# what changed since last commit
git diff HEAD
# diff between branches
git diff feature master
Think of a situation you have checked out on a
master branch and found a code bug. You start making changes to fix that and in the meantime you realised that you are directly making changes in master!
You thought to press
ctrl+Z but you don't want to loose the potential solution. In this situation branching can help. Checkout your solution with new branch and keep it in new branch until you are ok with the solution. This is simple scenario but there can be number of others. Branching is beautiful just you need to know, how to handle it!
# Checkout a branch
git checkout <existing branch>
# Checkout and create a new branch
git checkout -b <new branch name>
Consider you have few changes on branch
features/myFix and you want to merge it in
features/BugRel . All changes are in a folder.
# checkout your features/BugRel branch
git checkout features/BugRel
# pick changes from folder
git checkout features/myFix -- MyFixChanges/
That's all you have to do. Now that folder is in your branch, commit it.
git branch -m <oldname> <newname>
git branch -d <branchname>
# force delete
git branch -D <branchname>
Fast-forward - Because there was no conflicting activity in the target branch when we are merging changes from source, commits happen in sequence
git checkout <target branch>
git merge <source branch>
Use diff always before merging
git diff <target branch> <source branch>
Before starting merge always update from remote.
Aborting a merge
git merge --abort
An advance feature, Clean-up your local history(Squashing multiple commit into one) or pull changes from a branch into your branch without performing a merge. Use it only
your working branch not on public branch.
git log --oneline
git rebase -i <sha>
# to visit what happened after rebase
Cherry picking is the act of picking a commit from a branch and applying it to another.
# find the commit details
git log <branch name> --oneline
# checkout the branch where you want to put the commit
git checkout <b-name>
# perform the cherry pick commit to HEAD
git cherry-pick <commit>
That's All for this post. Thanks