WRITELOOP

GIT - BRANCHES AND MERGE

2012 June 5

BRANCHES

  • All git commits have a “SHA1 ID”. That code identifies a commit.
  • There is a default development “tree” (branch) on all git repositories: MASTER.
  • When you turn on any PAST point of that tree, git automatically puts you on a new branch, that can be named and saved.

To go back to a “past” commit:

$ git checkout -b <nome_do_novo_branch> <SHA1_ID_OF_DESIRED_COMMIT> That will create a new branch that will be the current one. You can commit on that branch, everytime you want. But you are on a new tree, that does not make part of the “default” tree (master). The commit’s SHA1_ID must not be fully typed (I’ve seen cases on the docs where just 4 chars were enough). IMPORTANT: if you make changes on that branch and do not commit them, when you change branch those modifications will not be brought to the destination branch.

To go back to the main tree (point in the “present”):

$ git checkout master

To temporarily go back to another branch:

$ git checkout <branch_name> There you can make the desired changes, commit and go back to master or another branch.

MERGE

Is the act of incorporating all commits from a branch into another one. E.g.: Merge the “TEMP” branch into the “MASTER” branch. Generally a commit has just one “parent commit” (which is the commit prior to itself). The merge operation results on a commit with at least 2 “parents” (also a commit can have multiple “parents”). During the merge process, the current branch becomes the “first parent” - almost always you’re not interested on the changes you made to the current branch (first parent), but indeed you are on the commits from other branches (the other “parents”) that can conflict with the current branch. NOTE: the “git pull” command automatically resolves merges, when possible. The “git pull” command gets the remote repository commits and “merges” them into your current branch. If you have not made any changes on your current branch, the merge is called a “FAST FORWARD” (analogue to getting the last version on a centralized Version Control System). But if you made changes on your current branch, git will automatically try a merge, and will report any conflicts found.

HOW TO MAKE A MERGE

A) Come back to the desired branch. E.g.: master

$ git checkout master

B) Merge it with the desired branch (e.g., I wanna bring the “fixes” branch

commit) $ git merge fixes That will bring into “MASTER” all commits made to the “FIXES” branch. Git will put those changes on the staging are (will do a “git add”), but will not commit the changes. So, you must do a git status to see what will change. After you verify and are OK with the changes, just do a “git commit” to make the merge effective.

MERGE CONFLICTS:

Suppose the following situation: $ git merge master Auto-merged layouts/default.html CONFLICT (content): Merge conflict in layouts/default.html Auto-merged index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. I can follow the steps below to fix those problems:

  • SOLUTION 1: Manually edit “layouts/default.html” and “index.html”: Manually check each file on a text editor. You will find marks “=====…” on the file. Before the “=====…” there are the changes of the CURRENT branch, and after that there are the changes from the branch I want to merge into the current one. You must make the manual changes, save the file and proceed with git add / git commit for each file.
  • SOLUTION 2: Discard the changes made on the file that correspond to the branch I want to merge. On Git GUI: Select the file, then Commit, Revert Changes.
  • SOLUTION 3 (GIT >=1.6.1): E.g. Keep the original index.html (–ours) and use the merged file (–theirs) for layouts/default.html: $ git checkout –ours index.html $ git checkout –theirs layouts/default.html IMPORTANT: No matter the chosen solution, you must always git add/commit each file, until all conflicts are solved and the merge complete.

MANAGING BRANCHES:

CREATING A NEW BRANCH (without making it the current one):

$ git branch <new_branch>

CREATING A NEW BRANCH AND SWITCHING TO IT:

$ git checkout -b <new_branch>

CHANGING THE CURRENT BRANCH:

$ git checkout <branch_name>

DELETE A BRANCH:

$ git branch -d <branch_name>

RENAME A “MASTER” OR WHATEVER BRANCH:

E.g.: Renames “master” to “part2”: $ git branch -m master part2

CREATE A NEW MASTER FROM THE CURRENT BRANCH AND SWITCH TO IT:

$ git checkout HEAD~7 -b master

LIST ALL BRANCHES:

$ git branch

REFERENCES:

TAGS