2

I'm new to GIT and I've a unique problem with respect to identifying a specific commit details. Can anyone please help me with the answer.

I've a master trunk called "M" and I've created branch called B1 from master trunk and in B1, I've committed code 3 times with commit IDs C1, C2, C3.

Now I need to find the following details.

  1. if I'm in C3, how can I find the branched out commit ID of B1 before C1 commit?
Shreyas
  • 63
  • 1
  • 10

5 Answers5

7

Suppose you have the following situation:

C0 --- C4 --- C5          <= master
   \
    \--- C1 --- C2 --- C3 <= B1

If you execute:

git log --oneline master..B1

You'll get:

C3 COMMIT3-MESSAGE
C2 COMMIT2-MESSAGE
C1 COMMIT1-MESSAGE

Then if you execute:

git rev-parse C1^

You'll get what you want:

C0
  • How to get the same details from GitHub repo using git commands. I don't want to clone all the files of the repo. I will clone only the files of a branch. – Shreyas Aug 03 '17 at 09:01
  • Execute "git clone --branch BRANCH --single-branch REPOSITORY" to clone only the history leading to the tip of a single branch. – Marcelo Ávila de Oliveira Aug 03 '17 at 11:29
1

Just enter git log and you will find all the commits in the order with the commit SHAs, assuming you are on branch B1. If not, do a git checkout B1 and then git log.

If you want the commits on master branch, do a git checkout master first and then enter git log.

nj2237
  • 1,220
  • 3
  • 21
  • 25
  • I want to know only the commit ID after which branch B1 was created. What will be the git command – Shreyas Aug 02 '17 at 11:24
  • On master branch, the commit IDs end with the last commit to master (ie. before branch `B1` was created). So if you want to know the commits IDs after `B1`, you need to go into `B1` first - do a `git checkout B1` for that, and then enter `git log`. – nj2237 Aug 02 '17 at 11:27
  • 1
    A branch is not made AFTER a commit. A branch is simply a pointer to a commit after all other commits are made. I think you should take a look at the last link in my answer. That is a very useful log alias which shows the exact commits all your branches are. Also you might want to take a look at http://learngitbranching.js.org/ which is a great explanation on how branching works. – Alen Genzić Aug 02 '17 at 11:27
  • @Shreyas If you want to view only the last `3` commits to `B1`, then use `git log -n 3` after `git checkout B1`. – nj2237 Aug 02 '17 at 11:29
  • Also, your terminology was quite confusing. I suggest reading the link @AlenGenzić has pointed out. – nj2237 Aug 02 '17 at 11:32
0

To see a list of all the previous commits you have made on your current branch you can run the following command:

git log

I find it easier however to add some additional parameters to make the log shorter and more concise

git log --oneline --graph -n 10

10 being the number of commits you want to see

Or for a even better git log, try this: https://coderwall.com/p/euwpig/a-better-git-log

Alen Genzić
  • 1,398
  • 10
  • 17
0

I find the following command very useful when I want to view all branches in my repo:

$ git log --all --graph --oneline --decorate

This will show a visual representation of the commit history for every branch in graph form. In addition, the commit at the tip of each branch is annotated with the branch name. You should be able to easily pick out the commit which you are looking for.

Note that this is a manual solution. If you are looking for a solution as part of an automated process, one of the other answers is probably better.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Parallel to the current solution, you can also use the following command:

git merge-base <feature-branch> main

It will give you the parent commit where branch was created. If in remote you are using master branch, replace main with master.

E. Erfan
  • 1,239
  • 19
  • 37