branches (git)

creating a branch

branches in git are different from mosth other vcs. in git, a branch is just a pointer to another commit and therefore this is very lightweight.

to create a new branch use the git branch command:

git branch testing

switching to a branch

now the branch is created, however your HEAD (your current location) is still pointing to the old branch. to switch to the newly created testing branch use the checkout command:

git checkout testing

creating and switching to a branch

to create and directly switch to a branch, use the -b flag in the checkoutcommand:

git checkout -b testing

merging branches

to merge brnaches use the merge command:

git checkout master
git merge testing

delete branches

if you dont need a branch anymore, you can delete it using the -d flag on the branch command:

git branch -d testing

merge conflicts

sometimes it can happen, that a merge doesn't go smoothly. this happens because you edited a file in both branches. git now is not sure anymore on what branch to pick from.

if you try to merge this, it will fail:

git merge testing
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

git has not merged the commits. it has paused the process while you resolve the conflict. to see which files are unmerged run git status:

git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

    both modified:      index.html

no changes added to commit (use "git add" and/or "git commit -a")

any file that has merge conflicts and hasn't been resolved is listed as unmerged. git automatically adds conflict-resolution markers into the file so the user can open them manually and resolfe the conflicts:

<<<<<<< HEAD:index.html
<div id="footer">contact : email.support@github.com</div>
=======
<div id="footer">
 please contact us at support@github.com
</div>
>>>>>>> testing:index.html

this means that on the HEAD (usually the main or master branch) is the top block and the testing branch is the bottom block. to resolve the issue delete the whole block and replace it with:

<div id="footer">
please contact us at email.support@github.com
</div>

after resolving the issues run git add to mark the file as resolved.

if you don't want to resolve the merge conflicts manually, run git mergetool. this is a tool that walks you through the conflicts.

after exiting the merge tool, git asks you if the merge was successful. if you awnser with yes, the file will be marked as resolved for you. to verify that all conflicts have been resolved run git status again:

git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    modified:   index.html

to merge the changes run git commit.

changing a branch's name

to change a branch name use the --move flag on the branch command:

git branch --move some-bad-name some-better-name

this change is just local for now. to push it to a remote use:

git push --set-upstream origin some-better-name

note that the some-bad-name branch is still present on the remote. delete this using the following command:

git push origin --delete some-bad-name