Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Using git Branches


Git
David Egan

Git branches are a good way to work on a feature or a bugfix.

You can create and switch to a new branch, which allows you to work without affecting the production code. Once happy wit changes, the branch can be merged back and deployed.

Determine & Switch Branches

Enter git branch. Returns branches, current branch indicated.

To switch:

git checkout <branchname>

Create New Branch (Local)

To create and switch to a new branch:

git checkout -b <newbranchname>

Don’t forget to git push origin <newbranchname> rather than pushing to master, which will return an “up to date” message.

The remote repo will add the new branch when the local is pushed.

See Which Files Have Changed

Check to see which files have changed:

git diff --name-status master..<branchname>

# even more info:
git diff --stat --color master..<branchname>

Merge Branch into Master

  1. Switch to master branch: git checkout master
  2. Merge: git merge <branchname>

If the master has changed since the branch was created, this will result in a merge conflict.

Dealing With Merge Conflicts

Merge conflicts generally result when the same part of a file has been changed by different branches.

During the merge, git will generate a useful merge-conflict message highlighting the file with the conflict. Running git status will show information about the conflict.

Open the relevant file - git adds conflict markers to the relevant code block, highlighting the different code by branch:

<?php
// organise-posts.php
<<<<<<< HEAD
$colour = "red";
=======
$colour = "green";
>>>>>>> bugfix

Just delete the code you don’t want, or add an entirely new block. Then git add and commit the file with a new commit message:

git add organise-posts.php
git commit -m "Resolve merge conflict: set things up properly."
git push origin master

Delete Branch

  1. Delete local branch: git branch -d <branchname>
  2. Delete remote branch: git push origin --delete <branchName>

More obtuse way of remote deletion: git push origin :<branchname>

Remove a Directory

Remove from git (e.g. Bitbucket, GitHub) but leave in place on local filesystem:

git rm -r --cached my-image-folder

Combine this with an entry into .gitignore to exclude directories (or files) from the repo.


comments powered by Disqus