a1an
a1an

Reputation: 3636

How can I git diff local changes to a different branch?

I have a repository checked out to some (unknown) tag ( Not currently on any branch. ) and with some local changes not checked in to the index. When I run git diff on a file it gives me some changes, which I am unsure with respect to which commit they are. I want to get the diff to the top of a given branch. Is there a git diff command for it? something like:

git diff myLocalFile myRemoteBranch

Upvotes: 5

Views: 1590

Answers (2)

Atemu
Atemu

Reputation: 406

Git has a tool built in for that:

git diff-index myRemoteBranch myLocalFile

It outputs in "raw" format by default, use -p to get a patch just like git diff does by default:

git diff-index -p myRemoteBranch myLocalFile

Upvotes: 0

Jonathan.Brink
Jonathan.Brink

Reputation: 25383

You can specify a particular file with diff by using -- at the end of the command.

Also, you can use the HEAD pointer to specify where you currently are, rather than having to specify the tag you are on.

Example:

git diff HEAD..myRemoteBranch -- relative/path/to/myLocalFile

Or, more simply:

git diff myRemoteBranch -- relative/path/to/myLocalFile

Also, if you actually want to compare against a "remote" branch, you should probably reference that branch using a remote like:

git fetch # retrieve latest from server
git diff origin/myRemoteBranch -- relative/path/to/myLocalFile

Upvotes: 5

Related Questions