Sathish Kumar
Sathish Kumar

Reputation: 2606

How to view 'only those commits' where were added since the repo was cloned?

I cloned a git repository

git clone xyz.git
  1. I modified a file (say 'a.txt'), added and committed it locally.
  2. Again, I modified a file (say 'b.txt'), added and committed it locally.
  3. I did NOT push the changes to remote

The git log produces the entire list of commits.

I would like to view only the commits which were added since the repository was cloned.

Is there any way to accomplish this?

Upvotes: 0

Views: 23

Answers (2)

Jeeter
Jeeter

Reputation: 6105

@torek brings up a good point, which is that it's tricky to tell when a repository was cloned. However, it's very easy to get a list of commits between your remote branch and your local branch.

Use

git log HEAD...origin/your_branch

HEAD is where the tip of your local branch currently sits.

origin/your_branch is where the repo has the tip of the remote branch


From the git help log page:

git log [<options>] [<revision range>] [[--] <path>...]

   <revision range>
       Show only commits in the specified revision range. When no <revision range> is specified, it
       defaults to HEAD (i.e. the whole history leading to the current commit).  origin..HEAD
       specifies all the commits reachable from the current commit (i.e.  HEAD), but not from origin.
       For a complete list of ways to spell <revision range>, see the Specifying Ranges section of
       gitrevisions(7).

Upvotes: 0

torek
torek

Reputation: 489858

"Since cloned" is difficult, or even impossible, depending on what information you specifically saved right after cloning, or how long it has been since then. But it's also not useful, so it doesn't matter how difficult it is: "since cloned" is the wrong question to ask. (For a very flawed analogy, imagine trying to catch an escaped animal. The interesting question is not where it was three weeks ago, but rather where it is now.)

When you have a remote—which is just a short name, like origin, for another URL where there is another Git repository that contains commits—the interesting question is not What do I have that they didn't back when I ran git clone? but rather What do I have that they don't have the last time I saw what they have? And that is a question Git will answer, because your Git records, in your repository, its memory of what they had the last time you connected to them.

If you have a branch master that your Git made from their branch master, your Git also has its memory of "where their master was, the last time we talked to them." That memory is stored in origin/master. Therefore, you can run:

git log origin/master..master

which means "show me what I have on my master, that is not also on origin/master".

If you run:

git fetch origin

(which you can do at any time), that tells your Git: Connect to the other Git over at origin, and pick up whatever new items they have, and update my origin/master and other origin/ remote-tracking branch names.

Similarly, when you run:

git push origin master

that tells your Git: Connect to the other Git at origin, give them my new commits, and ask them to set their master to the same thing as my master. If they—the other Git at origin—agree to the request to update their master, your Git records that in your origin/master, since your Git knows their Git is updated.

Upvotes: 1

Related Questions