Skip to content

Instantly share code, notes, and snippets.

@aogilvie
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aogilvie/cddeeb0b415d79a77b27 to your computer and use it in GitHub Desktop.
Save aogilvie/cddeeb0b415d79a77b27 to your computer and use it in GitHub Desktop.
Splitting a large commit after having created a PR

Scenario

Working on a feature branch, let's say "feature/local-notification". Once finished, one commits all the files and sends the PR to let's say "develop" on a remote repositiory used by your team.

When a reviewer comes to check the PR they announce they are unable to comment on diffs or view all the diffs on Github as Github's diff limit was reached.

You are asked to (kindly) split the commit...

Below I will outline the (what I think is the best and most flexible) method of doing this (there are perhaps other ways depending on your situation).

Solution

log the commit hash of the commit that was pushed and was too big (assuming you are still on "feature/local-notification");

git log

(if it was your last commit it'll be at the top of the log list)

What we will do is simply git checkout to a clean branch of the remote like "develop" or "master" branch etc) and re-apply the pushed commit via git merge but prevent the merge from actually commiting (sounds complicated, it's not!).

Let's go to our previous commit (the commit before the monster commit) or clean branch:

git checkout <the commit hash or branch name>

From here you can decide to make a new branch if it's a new feature (up to you).

git checkout -b feature/local-notification-part1

From here grab that commit we noted from earlier (the commit we PR-ed but need to split):

git merge origin <the commit hash> --no-commit --no-ff

^ This command will fetch the commit, apply the diffs, but will not commit and will not fast-forward.

...do git status and BAM you should see something like this:

On branch feature/local-notification-part1
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

    modified:   www/somefile.js
	modified:   www/js/anotherfile.js
... lots of files ...
	modified:   www/views/somefile.html
	modified:   www/views/aFolder/somefile.html
	modified:   www/views/aFolder/somefile.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)

prompt>

Sweet, all our files before the commit, now we can unstage a file with git reset for example:

git reset www/somefile.js

git status

gives:

On branch feature/local-notification-part1
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

	modified:   www/js/anotherfile.js
... lots of files ...
	modified:   www/views/somefile.html
	modified:   www/views/aFolder/somefile.html
	modified:   www/views/aFolder/somefile.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   www/somefile.js
	
Untracked files:
  (use "git add <file>..." to include in what will be committed)

prompt>

We can now go about splitting up our files for our new commit! Rejoice and be merry :)

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