DEV Community

Arun Kumar Singh
Arun Kumar Singh

Posted on

My Git Notebook!

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.

Git Remote Server

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
Enter fullscreen mode Exit fullscreen mode

To track any remote branch

git checkout --track origin/feature
Enter fullscreen mode Exit fullscreen mode

receive changes from remote repo -
git fetch - download changes and then you can evaluate
git pull - combination of fetch and merge

Use diff

diff command is pretty useful while working with git.

# what changed since last commit
git diff HEAD
# diff between branches
git diff feature master
Enter fullscreen mode Exit fullscreen mode

Branching is helpful !

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>
Enter fullscreen mode Exit fullscreen mode

Picking specific changes(folder/file) from another branch

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/
Enter fullscreen mode Exit fullscreen mode

That's all you have to do. Now that folder is in your branch, commit it.

Rename/Delete Branch

git branch -m <oldname> <newname>
git branch -d <branchname>
# force delete
git branch -D <branchname>
Enter fullscreen mode Exit fullscreen mode

Merge and Compare Branches

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>
Enter fullscreen mode Exit fullscreen mode

Use diff always before merging

git diff <target branch> <source branch>
Enter fullscreen mode Exit fullscreen mode

Before starting merge always update from remote.

Aborting a merge

git merge --abort
Enter fullscreen mode Exit fullscreen mode

Rebase

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
git reflog
Enter fullscreen mode Exit fullscreen mode

Cherry Pick

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>
Enter fullscreen mode Exit fullscreen mode

That's All for this post. Thanks

Top comments (0)