Search code examples
gitbitbucketpull-requestrebasing

Squashing Commits to a single commit for a bitbucket pull request


I'm trying to work on a version of git flow that can be used for a small team of developers to manage our development process, most of the employees are both new to git and bitbucket. I have more experience with both but I'm by no means an expert. We need a single commit for pull requests to upstream develop and the process I've worked out so far is as follows:

Assuming an existing fork of a repository from upstream. Also, note that I know that some of these commands can be combined but I'm also trying to teach the basics first. This is how it's documented (more or less, I'm even more verbose in the actual documentation).

# checkout
$ git checkout develop

# update
$ git pull --rebase upstream develop 

# Create topic branch
$ git branch <topic branch name>

# Checkout topic branch
$ git checkout <topic branch name>

Do work

# Add changes to index
$ git add --all .

# Commit changes
$ git commit # and provide commit comment

Prepare for pull request, this branch is created so we can retain our commits before they were squashed, just in case the pull request is declined, or for whatever reason.

# Branch the topic branch for a pull request
$ git branch <topic branch>-pr 

# Checkout the topic branch
$ git checkout  <topic branch>-pr

# Rebase to squash commits
$ git rebase -i  # Rebase and provide a commit message
                 # for all of the commits that are squashed

# Something like the following is shown
pick 1fc6c95 do something  
pick 6b2481b do something else  
pick dd1475d changed some things  
pick c619268 fixing typos  

# Change the word pick to squash for all but the first line, for example:

pick 1fc6c95 do something  
squash 6b2481b do something else  
squash dd1475d changed some things  
squash c619268 fixing typos  

-> It's at this point, where I have a problem. I can't seem to get down to one commit. There are always at least two commits in this branch, I have seen the option to use --root but when I do that bitbucket complains that my pull request branch and the upstream branch are unrelated and I can't issue a pull request.

# Push branch to origin
$ git push origin <topic-branch>-pr 

I feel like I'm so close to having a working process, so any help would be greatly appreciated.


Solution

  • You are doing it properly, checkout: https://github.com/ginatrapani/todo.txt-android/wiki/Squash-All-Commits-Related-to-a-Single-Issue-into-a-Single-Commit

    In order to squash down to one commit - git rebase -i HEAD~4 in order to rebase the last 4 commits.

    Also, you should do a git-fetch or git-pull prior to checkout if it is a new branch. There is no reason to pull with a rebase before any work is done, you would only rebase after work has started and commits are made.