“Only YOU can prevent useless merges”
If you’re just starting out with Git, you’ll inevitably run into commits into your feature branches like the following:
Merge branch ‘test’ of git.lullabot.com:lbcom into test
What are these commits, and how did they get created? Usually, it’s the result of adding a commit to your local copy of a branch, and then pulling upstream changes into that branch. Since your local commit isn’t on the remote repository yet, when git pull runs git merge origin/[branch] [branch], it will automatically do a “recursive” merge and create a commit with the remote changes. Then, when you push your changes up, you end up with both a merge from the remote integration branch into your local branch, and a merge from your feature branch into the integration branch.
Let’s take a look at an example of how this situation can happen, and a way to resolve it cleanly. First, let’s create two temporary git repositories: “upstream”, to represent the remote repository, and “downstream”, to represent your local clone of the repository.
~/ $ cd /tmp
tmp/ $ git init upstream
Initialized empty Git repository in /private/tmp/upstream/.git/
tmp/ $ cd upstream
upstream/ $ git config —local receive.denyCurrentBranch ignore # This allows us to push into upstream even when it has a branch checked out
upstream/ $ echo ‘Demo for how to handle upstream commits after you have merged into the upstream branch.’ > README.txt
upstream/ $ git add README.txt
upstream/ $ git commit -m ‘Adding a README file.’