-Advanced

Commit Early, Commit Oftern!

Key concept: Merge Vs Rebase

  • Integrate changes from one branch to another
  • Once you are done with your feature, you merge it back into master
  • Every time you merge there will be a merge commit
$ git checkout feature

$ git merge master


OR


$ git merge master feature
Benefits:

* Existing branches are not changed in any way
* Preserve the complete history of your project and avoid the risk of re-writing public commits

Bad about Merge:

* Every time you merge, it will create a new commit for that, which can leads
  to polluted history

Key concept: Rebase

  • Integrate changes from one branch to another
  • It solves the same problem as git merge
  • Moves the entire branch to begin on the tip of another branch
  • Rewrite the project history by creating new commits
$ git checkout feature

$ git rebase master

Moved the entire feature branch to begin on the tip of the master branch

Benefits:

* Get a clean project history
* Eliminates the unnecessary merge commits required by git merge
* Gives a perfect linear project history

Disadvantages:

* If you don’t follow the Golden Rule of Rebasing, re-writing project history 
  can be problematic
* Loses the context provided by a merge commit — 

The Golden rule of Rebasing

  • Never use it on public branches
    

rebased master onto your feature branch

Key concept: Interactive Rebasing

  • Gives you the opportunity to alter commits as they are moved to the new branch
  • More powerful than an automated rebase
  • You can use it to clean up a messy history before merging a feature branch into master

pick 33d5b7a Message for commit #1
pick 9480b3d Message for commit #2
pick 5c67e61 Message for commit #3

# Rebase 2b4dba8..eb98833 onto 2b4dba8 (7 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
$ git checkout feature
$ git rebase -i master

Local Cleanup: Squashing/Dropping


$ git checkout feature
$ git rebase -i HEAD~3

Some important commands:

  • $ git commit --amend
    • amend the changes in last commit, don't create a new commit
  • git checkout commit_hash
    • checkout the repo state at that specified commit
  • git revert commit_hash
    • revert the changes by creating a new commit
  • git reset commit_hash
    • match the state of the repository at that specified commit

key Points:

  • Don't forget to use amend
  • You always have to follow Golden rule of Rebasing
  • You have to use --force push if your branch conflicts with remote branch

Thanks

Git Advanced

By Sushil Khanchi

Git Advanced

Advance concept of git (version control system)

  • 650