Doug
Doug

Reputation: 35216

How do I get the changes from a branch in git without the commits?

I'm looking for a less rubbish way of doing this:

git diff patch.DisableCreditCheck > ~/patch
patch -p1 < ~/patch

ie. The changes from a branch as raw uncommitted changes.

The downside of patch is that it isn't very clever; for example I often get "Reversed (or previously applied) patch detected! Assume -R[n]?" when there isn't any previous patch, it just happens that there's a set of filenames that it gets confused by.

It'd be much nicer to be able to do this:

git merge --no-commit --no-ff patch.DisableCreditCheck

...but that leaves you in a merge state, with no obvious way to get back to being in a normal state while preserving the changes.

Help! Any decent way of doing this?

(This is a very useful thing to be able to do when you're sharing patches on the code youre working on; much MUCH better than having a set of patch files to pass around)

Edit: As an example of what I'm trying to achieve, lets say we have two branches, master, and config.me.

When I do git diff config.me > ~/patch; patch -p1 < ~/patch the result is:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   Web/Web.config
#

This is the desired outcome... but without using patch, which doesn't work well (specifically on windows it seems incredibly bad at detecting changes that should merge perfectly, but instead it'll get confused and write out a bunch of .orig and .rej files).

So, like I said; I want the changes from a branch without the commits as local uncommitted changes.

Upvotes: 2

Views: 357

Answers (2)

antak
antak

Reputation: 20869

Your first example shows that you don't want the changes (i.e. commit deltas) of a particular branch, but rather you want your working-tree to reflect the latest state of that branch. (A subtle difference that can cause of confusion when trying to pick the right commands.)

Few ways:

Method 1:

$ git checkout patch.DisableCreditCheck .

This gets your working-tree to mirror patch.DisableCreditCheck. Changes between HEAD an patch.DisableCreditCheck are automatically staged. Word of caution: It'll also blow away any uncommitted changes in your working-tree. (Note: that period (.) needs to be the path to the root of your working-tree.)

Method 2:

$ git read-tree patch.DisableCreditCheck

This gets your index to mirror patch.DisableCreditCheck, but leaves your working-tree as is.

Method 3:

$ git checkout --detached patch.DisableCreditCheck
$ git reset your-original-branch
$ git checkout -B your-original-branch

This is a silly method that's roughly the same as Method 1. It's included for academy and because it might be a little easier to follow.

Upvotes: 1

Nils Werner
Nils Werner

Reputation: 36835

Two ways:

First, if you want the changes from branchA but don't want them to be committed in branch branchB:

>> git checkout branchB
>> git cherry-pick -n ..branchA

This will get all commits that happened "in between B and A" but doesn't commit them. git status will show a bunch of changed files.

Secondly, if you want to have the changes in a new commit but not necessarily every single commit you made:

>> git checkout branchA
>> git rebase -i branchB

This will rebase your branch A ontop of B but will let you chose wich commits to pick and wich ones to squash. Squashing will essentially collapse several commits into a single, bigger one. See git help rebase for more info on squashing.

Upvotes: 0

Related Questions