Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - compare local and remote without fetching first?

Tags:

git

compare

I've searched around and found no answer to this question.

I have an app running on Heroku. From my local machine I typically implement and just:

git add .
git commit -m "whatever change, I know I can add and commit at the same time..."
git push <the-heroku-repo>

Then it goes up and the master branch in the Heroku app is updated. So far so good.

Now. I want to have another machine that will automatically make a pull from the Heroku repo and update itself.

So i do that with:

git clone <the-heroku-repo>

That gets me the app and I can see the git config with this:

core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
[email protected]:theapp.git
branch.master.remote=origin
branch.master.merge=refs/heads/master

To update this new repo I can do just a pull:

git pull origin

Or I could fetch and merge:

git fetch origin
git merge origin/master

MY QUESTION

Between the above fetch and merge I can check what are the changes by doing:

git log -p master..origin/master

Is there a way to find the differences between the local master branch and the latest version in the remote Heroku repo without fetching before? Just compare the local and remote and see the changes. I just can't find the proper way.

Thanks.

like image 357
Pod Avatar asked Sep 27 '11 14:09

Pod


People also ask

How does git compare local and remote files?

To compare the local and remote branches in Git, first, open up the Git terminal and execute the “$ git fetch” command to fetch and update the remote branches. Then, run the “$ git branch -a” command to display all remote and local branches.

How can you tell the difference between local and remote?

The git fetch command will fetch all changes that happened in the origin. And the git diff will show us the differents files between our working tree and the remote.

Does git fetch affect remote?

git fetch really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository.


1 Answers

Although you can get some summary information about the branches in the origin repository using:

git remote show origin

... you do need to fetch the branches from origin into your repository somehow in order to compare them. This is what git fetch does. When you run git fetch origin, it will by default only update the so-called "remote-tracking branches" such as origin/master. These just store where the corresponding branch was at in origin the last time you fetched. All your local branches that you've been working on are unaffected by the git fetch. So, it's safe to do:

git fetch origin
git log -p master..origin/master

... and then if you're happy with that, you can merge from or rebase onto origin/master.


I would encourage you not to worry about the resources (either disk space or bandwidth) involved in the git fetch origin command. git efficiently sends just the objects that are necessary to complete the remote-tracking branches that are being updated, and unless you have unusually large files stored with your source code, this shouldn't make much of a difference. In addition, it's frequently useful to have the complete history of the branches from the other repository available even if you don't plan to use them, for example so you can examine that development history.

like image 130
Mark Longair Avatar answered Sep 23 '22 05:09

Mark Longair