Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created November 16, 2013 09:55
Show Gist options
  • Save stevenyap/7498291 to your computer and use it in GitHub Desktop.
Save stevenyap/7498291 to your computer and use it in GitHub Desktop.
Git Branching

Git Branching

Problem

  • You have pushed your code to production and you are going to work on a major story.
  • Halfway through, the client called to ask you to fix a major bug immediately.
  • Your current code base is half-way, but the bug is urgent. How to resolve this?

Branching and Merging to the rescus

  • You should work on the new story on a new branch
  • In the happy event (where there is no sudden issues), you can simply merge back the new branch into master just like normal pushing
  • In the sudden event where you need to fix a bug on the master branch, you can simply switch to the master to fix the bug and then push it, while leaving your new branch intact. You will then merge back the new branch into master later when you are done.

Creating branch

  • Creates the new branch called v2.0.1b
git branch v2.0.1b

Switch to the branch

git checkout v2.0.1b

# You can now commit to this branch
git commit -a -m "Some new commits"

# There is no need to push to origin as this branch is meant to be merged back to github
# However, if you want, you can do this:
git push origin v2.0.1b

You are in branch v2.0.1b and you can work on your new story in this branch

Happy path: Work completed without intervention

  • You can now merge your branch back to master
git checkout master
git merge v2.0.1b
git branch -d v2.0.1b # deletes the branch since it is not needed anymore

Sad (but not that sad!) path: Work incomplete but need to fix bugs on master branch

  • You commit your current code in v2.0.1b branch (Important!! As unstaged files will conflict in the master branch)
  • You will change back to master branch
  • Fix the bugs on master branch
  • Commit to master branch
  • Complete v2.0.1b branch and then merge back
git checkout master

# fix bugs on master branch... done

git commit -a -m "Fix bugs on master branch"

# go back to work on v2.0.1b branch
git checkout v2.0.1b

# Completed work on v2.0.1b branch
git checkout master
git merge v2.0.1b
# Resolve conflicts until okay (see Git Merge Code.md for info)

# Push branch to master
git commit
git branch -d v2.0.1b # delete the branch as it is no longer needed

Notes

  • Branch is created locally and will not be reflected in origin
  • If you want to store the branch in origin, you need to git push origin <branch_name>
  • If you want to delete the remote branch, you do git push origin :<branch_name>
  • If you want to delete the local branch, you do git branch -d <branch_name>
  • If you want to stop tracking a remote branch, you do git branch -d -r origin/<remote branch name>
  • You can see a local branch as only for local usage and will be deleted when you merge it back to master

Tips

  • Run git branch --merged to see branches that are merged with master so that you can delete them
  • Run git branch --unmerged to see branches that are NOT merged with master
  • Run git push --all origin to push all your branches in one shot

Branching Concepts

  • Master branch is the most stable and viewed the commit pushed to production (ie. It should be always deployable)
  • Develop branch is the branch that are being push to staging for acceptance
  • Topic branch is the branch where coder test codes/ideas
  • Develop branch is merged back to master branch as it becomes stable and ready for production
  • You can also create a staging branch which is deployable to staging server until it is ready for production. This branch should be merged with Master before pushed.

Branching Concepts

@PubgGamerUzbSila
Copy link

no no no

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment