Git / Bitbucket Feature Branches
Official Documentation
Required Reading:
Create a Feature Branch
Command-line Version
- Clone the main project:
cd ~/workspace
git clone git@bitbucket.org:path2response/co-op.git
cd co-op
- Create branch:
git branch <new-branch>
- Or create and checkout in one command:
git checkout -b <new-branch> <from-branch>
<from-branch> defaults to the branch you are currently on.
Using Bitbucket
- Go to the project in Bitbucket
- Click “Create Branch” button on the left
- Follow the instructions
Common Git Operations
Switch to a Branch
git checkout <branch-name>
<branch-name> can be main.
Sync Branch with Main
While on your branch (if local main is up to date):
git merge main
Merge Branch Back to Main
While on main:
git merge <branch-name>
Delete a Local Branch
git branch -d <branch-name>
Delete a Remote Branch
git push origin :<branch-name>
Full Graphical Comparison Between Branch and Main
Setup Your Git Difftool
In ~/.gitconfig:
[merge]
tool = opendiff
[diff]
tool = opendiff
- Mac:
opendiff(comes with XCode) - Linux:
meldworks well
Run Comparison Script
#!/bin/bash
# TODO Parameterize
branchname=PATH-1529
rm -rf ~/temp/diffs/$branchname
mkdir -p ~/temp/diffs/$branchname
cd ~/temp/diffs/$branchname
git clone git@bitbucket.org:path2response/co-op.git
cd co-op
git fetch
git checkout $branchname
git difftool main..$branchname
Quick Reference
| Action | Command |
|---|---|
| Clone repository | git clone git@bitbucket.org:path2response/<repo>.git |
| Create branch | git branch <branch-name> |
| Create and checkout | git checkout -b <branch-name> |
| Switch branches | git checkout <branch-name> |
| Sync with main | git merge main (while on feature branch) |
| Merge to main | git merge <branch-name> (while on main) |
| Delete local branch | git branch -d <branch-name> |
| Delete remote branch | git push origin :<branch-name> |
| Push branch to origin | git push -u origin <branch-name> |
| Pull latest | git pull |
| Fetch and prune | git fetch -p |