Open In App

Git-Checkout And Merge

Last Updated : 30 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will mainly cover the basic git commands that are helpful in branching and merging. If you want to know what is git branch, go through this article: Git-Introduction to Branch. and An Ultimate Guide to Git and Github. These two articles will help you to brush up some of the previously discussed concepts.

git Checkout: The git checkout is navigator command that helps to switch branches. This option prepares for you to work on a particular working branch. It Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch.

The git branch and git checkout commands are like best friends who always hang out together as they always work hand-in-hand. git branch command is used to create new branches, display existing branches and git checkout is used to switch to specified branch.

git checkout options: Below is a list of some of the basic git checkout options with the visual example.

  • git checkout “New Branch”: It is used to navigate between branches.
  • git checkout -b(or -B) “New Branch”: Specifying -b causes branch option to be executed and simultaneously creates and checkout New branch. If -b is given, “New Branch” is created if it doesn’t exist; otherwise, it is reset.
  • git checkout -q(or –quiet): It suppresses the feedback messages.
  • git checkout -f(or –force): When switching branches, it enables to proceed even if the index or the working tree differs from HEAD.
  • git checkout –detach: This option rather than checking out a branch to work on, check out a commit for inspection experiments. This is the default behavior of git checkout “commit” when “commit” is not a branch name.

git merge: The git merge command allows you to join two or more development work created using git branch into a single branch. It incorporates the changes from named commits and diverges them into the current branch. Before making merge option Make sure the receiving branch and the merging branch are up-to-date with the latest remote changes.
git merge options: Below is a list of some of the basic git merge options.

  • git merge –commit: This option is used perform merge along with commit.
  • git merge –no-commit: This option perform the merge and stop just before creating a merge commit, to give the user a chance to inspect merge result before committing.
  • git merge –edit: This option invoke an editor before committing successful merge and enable the user to justify the merge instead of auto-generated merge message.
  • git merge –no-edit: This is used to accept auto-generated messages only.
  • git merge -m “msg” : This set the commit message to be used for the merge commit.
  • git merge -ff: When the merge resolves as a fast-forward, it only update the branch pointer, without creating a merge commit.
  • git merge -no-ff: Contrary to default behaviour, it create a merge commit even when the merge resolves as a fast-forward.

Fast forward merge: Fast forward merge occurs when current branch head is an ancestor of the mentioned commit. In this case, a new commit is not needed to store the combined history; instead, the HEAD is updated to point at the named commit, without creating an extra merge commit. However, This can be ignored with the –no-ff option.

Let us try to understand the workflow of a git branch, git checkout and git merge

  • We create three text document namely; Even-numbers.text, Odd-Numbers.txt and Prime-Numbers and listed first five even, odd and prime numbers respectively.Now we put them under version control using git.
  • We create another branch namely “next-five-even-odd” using git branch “New-branch” to work separately from master branch and add next five even and odd numbers in the their respective document.
  • Now using git checkout command we switch to “next-five-even-odd” branch.
  • We update the list of even and odd numbers and commit the changes using git add and git commit commands.Now we see that changes are committed to “next-five-even-odd” branch, the one on which working on.
  • When we switch back to master branch we observe that master branch remain unaffected by the changes done in any other branch.
  • Now, we merge the two branches i.e.”master” branch and “next-five-even-odd” branch. using git merge command.
  • Now what all changes were committed to “next-five-even-odd” branch also reflects at our “master” branch

Summarizing: In this article we learnt that git checkout command is used to change or switch branches. This command works hand-in-hand with git branch command.git merge command join together the different workpaths into single workpath. These commands are essential commands for git workflow.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads