GIT: interact between two branches

Wow, this is one of the best things I’ve discovered in recent history. Not the best, that’s something completely different, but certainly one of the best related to work. It’s a shorthand that has already saved me countless clicks and keystrokes.

Let me set up a scenario (#1):

  1. Set up a git repository somewhere (local, GitHub, Bitbucket, Beanstalk, etc)
  2. Switch away from develop and master to work on a feature
  3. Test
  4. Commit to the feature branch
  5. Switch to develop and merge feature branch to test again on the staging environment. Alternatively have someone do your pull request to merge instead of doing this yourself.
  6. Either switch back to the feature branch to continue working on the feature or toss it.

Another scenario (#2):

  1. Set up a git repository somewhere (GitHub, Bitbucket, Beanstalk, etc)
  2. Switch away from develop and master to work on a feature
  3. Someone pushed something to base (develop) and you wish to update your feature branch with this new thing
  4. Merge develop to feature branch and continue working (ideally without conflicts)
  5. Commit to the feature branch
  6. Switch to develop and merge your feature branch
  7. Switch back or toss the feature branch.

The steps in this may seem a bit redundant, but some standards require certain extra steps to ensure quality and control. Also it helps releasing in increments. Anyway, the list of commands per scenario:

#1

git clone ssh://user@repository
git checkout feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
# do some work
git add . (or git add src/humans.txt or git add -p)
git commit -m "Added the new kid to humans.txt"
git push
git checkout develop
git pull
git merge feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
git push

#2

git clone ssh://user@repository
git checkout feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
# do some work
git checkout develop
git pull
git checkout feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
git merge develop
git add . (or git add src/humans.txt or git add -p)
git commit -m "Added the new kid to humans.txt"
git push
git checkout develop
git pull
git merge feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
git push

Now the awesome: Anytime you’re working with two branches and need to switch between them, you can simply use – instead of copy pasting the name or tabbing to autocomplete. It saved me a lot of annoying mouse-grabbing and tabbing to get feature branches. The minus is a shorthand for git checkout @{-1} – or “last branch used”.

$ # switching from develop to another branch
$ git checkout feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
On branch feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
$ git checkout - # and back
On branch develop
$ git checkout - # and forth
On branch feature-awesome-new-feature-with-a-long-branch-name-to-keep-track-of-things
$ git checkout - # and back again
On branch develop

Helping anyone out here? My two cents today! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.