Using git Branches
Git
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:
Create New Branch (Local)
To create and switch to a new branch:
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:
Merge Branch into Master
- Switch to master branch:
git checkout master
- 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:
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:
Delete Branch
- Delete local branch:
git branch -d <branchname>
- 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:
Combine this with an entry into .gitignore
to exclude directories (or files) from the repo.
comments powered by Disqus