Skip to content

Git Merge Workflow

Siddharth Parmar edited this page Dec 2, 2019 · 1 revision

Reference: https://nvie.com/posts/a-successful-git-branching-model/

.gitconfig (optional)

Put the following in .gitconfig in your home directory

[user]
  name = <Your Name>
  email = <Your Email>
[diff]
[color.diff]
  meta = yellow
  new = green reverse
  whitespace = red reverse
[color]
    ui = auto
[alias]
    st = status
    ci = commit --verbose
    co = checkout
    di = diff -w
    dc = diff --cached -w
    noff = merge --no-ff

You can save a few keystrokes now by using git co -b instead of git checkout -b.

Getting the latest develop branch

  1. git checkout develop
  2. git pull --rebase origin develop

Starting a New Feature

  1. Get the latest develop branch
  2. git checkout -b [your initials]-feature-my-new-feature
  3. Make commits...

Prepare for a Pull-Request

  1. Commit all your work into your feature branch (git status should show no modifications)
  2. Squash your commits (Optional)
  3. Get the latest develop branch
  4. git checkout feature-my-new-feature
  5. git merge --no-ff develop (this merges new commits from develop into feature-my-new-feature)
    • Solve any conflicts,
      • use git add <conflicted-file> to mark conflict resolved
      • use git commit to continue with the merge
  6. Test to make sure your feature is still working
  7. git push origin feature-my-new-feature

If you need feature-someone-else-super-feature and it's not merged to develop yet...

  1. Commit all your work into your feature branch (git status should show no modifications)
  2. git fetch origin feature-someone-else-super-feature
  3. git merge --no-ff feature-someone-else-super-feature
    • resolve any conflicts
  4. Test to make sure your feature is still working

If something doesn't work as intended and you want to bail

  • If the merge hasn't committed yet, you can do git merge --abort
  • If you have already merged
    • use git log --oneline to identify the hash of the commit before the merge commit
    • make sure you are on your own feature-my-new-feature branch
    • make sure there's nothing not committed (git status)
    • git reset --hard <commit you want>

Squashing Commits (Optional)

Optional (if you have too many commits on your feature branch)

  1. git log --oneline
    • Copy the commit hash previous to your first commit on the feature.
  2. Squash your commits: git rebase -i [paste commit hash]
    • You'll see a list of your commits next to the word pick
    • Change all pick to s except the topmost pick
    • Save and exit the VIM editor.
  3. Create a commit message for your squashed commits
    • Remove all lines not beginning with #.
    • Add a commit message at the top of the file (no # in front).
    • Save and exit the VIM editor.

Creating a Pull-Request

  1. Locate your feature branch in the Branches tab in your GitHub repo.
  2. Click Create Pull Request and leave a short description of the feature you finished.

Responding to a Change Request

To update your pull request with changes, simply continue making commits in your feature branch.

Do not squash these new commits (Or, you'll end up with conflicts). Instead, just push them to GitHub as normal.

  • When your branch has been merged, remove your local feature branch git branch -d local-feature-branch

Solving Pull-Request Conflicts

  1. Get the latest develop branch

If there are conflicts with develop use the following command

  • git checkout . && git reset --hard origin/develop

If not continue to the next step

  1. git checkout feature-my-new-feature
  2. git merge --no-ff develop
    • Solve any conflicts and git add -A, then git commit to continue merge
  3. git push origin feature-my-new-feature (If you're unable to push, try git push -f origin feature-my-new-feature to force)

Happy Coding! ✌️