Tuesday, July 5, 2016

Unleashing the Git - part 6 - git branching

Branching is a function to launch separate, similar copy of the present workspace for different usage requirements. It's a line of development which exists independent of another, where both share a commom history.

Let's understand branching related commands with an example.
$ git init        // Execute on a preferred location
Add a file list.txt and add the following content


$ git add .
$ git add -m "Initial commit of list.txt"
$ git checkout -b mybranch  // Create a new branch called mybranch with exact level of master.
$ git checkout -b 64868c9   // Create a new branch called mybranch from the point of the given commit id
Above is equivalent to following two commands.
$ git branch mybranch
$ git checkout mybranch
$ git branch   // Shows available branches with the current branch with an *. Now you'll be in mybranch
$ git branch -r // Shows only remote branches.
$ git branch -a // Shows all available branches
$ git branch --merged // Shows all branches merges into the current branch
$ git branch --no-merged // Shows available branches that are not merged with the current branch
$ git branch --contains <commit id> // Shows the branch that contains the commit id
Now suppose you openned list.txt and modified it by adding a new line, making it

$ git add .
$ git commit -m "Adding a new line in the mybranch"
$ git checkout master  // Switch back to master. Now list.txt won't have your new line
$ git merge mybranch  // merges mybranch with the master branch. This would succeed as there was no conflict in lines. We just added a new line, so git would have no problem in merging. Updating 53a7908..3fd44bc Fast-forward  list.txt | 1 +  1 file changed, 1 insertion(+) Also git automatically commit while merging. If you do not want the commit to happen, you can use the following command.
$ git merge --no-commit master
// Now in the master branch remove the first line and commit it. Then switch to mybranch and open list.txt. You would still see the initial first line we removed in the master. Now modify that line in mybranch to change it to xyz from abc and commit it. Say now you want to merge the files in this branch from master. Issue the following command.
$ git merge master
Auto-merging list.txt
CONFLICT (content): Merge conflict in list.txt
Automatic merge failed; fix conflicts and then commit the result.
There will be a conflict as both branches have modified the first line.

// Now if you try to switch to master, you won't be allowed until you fix the conflicts.
$ git checkout master
list.txt: needs merge
error: you need to resolve your current index first
Now open the list.txt in mybranch. You would see following.
<<<<<<< HEAD
>>>>>>> master
What does this say? HEAD is pointing to your current branch. i.e. mybranch. Whatever you have between <<<<<<< HEAD to ======= is the the contents of the mybranch offending to the conflict. i.e. your xyz modification. Whatever between ======= and >>>>>>> master is what master has for the offending conflict. In this case it's empty meaning there's an empty line.

Now you may decide to keep the xyz change and commit it. What needs to exist, what needs to remove is all up to you. You may even choose remove both. So once you switch to master branch, you won't see the line. Issue the following command in master branch.
$ git merge mybranch
Updating a64cb6d..87fd36d
 list.txt | 1 +
 1 file changed, 1 insertion(+) 
Now you should see the xyz line in the master branch as well.

Following are few additional commands that is are useful.
$ git merge --log development // Adding a one line log message from each merge commit to the merge message
$ git merge --no-ff development // Force creation of a merge commit
$ git merge -m "Forced commit message that from the merging branch" mybranch
You can use git commit --amend to modify the commit message after the fact too. Here’s an example:
$ git merge --log --no-ff development
$ git commit --amend -c HEAD  // Editor launches
$ git branch -d mybranch  // Deletes the mybranch. If there are unmerged files, git will warn you about it.
$ git branch -D mybranch // Git doesn't warn you about unmerged files. It just deletes the branch
$ git push origin :beta // Remove a remote origin's branch named beta
$ git push <remote name> <branch name>
$ git push origin master:production  // Push changes from local master branch to remote origin's production branch
$ git push --force or git push -f   // To push changes even  if you don't have ceratin changes from the remote. Warning: Use this with extreme caution because it can cause others to get
out of sync with the repository to which you are pushing.

As an alternative to 'git pull', we can also use 'git fetch' followed by 'git merge @{u}'

Some more useful commands
$ git checkout -b <branch> --track <remote>/<branch> // Checkout a remote branch to your local filesystem
$ git pull <remote name> <remote branch>   // Pulls changes from the <remote branch> of <remote name> into the current local branch
$ git pull <remote name> <remote branch>:<local branch> // Pulls changes from the <remote branch> of <remote name> into the <local branch>
    // e.g: git pull origin development:team-dev
$ git pull --rebase origin master // rebasing the current locak branch on top of remote origin's master branch

No comments:

Post a Comment