2

I need to create a new branch, from the existing branch using the git checkout and option start-point but I am not sure how I can determine it properly. from git log I need to find a commit which has specific transaction number in the message. E.g. from git log

..........................................
commit b91a725feea867e371c5488e9fe60ca4cd0e927f
Author: john.smith
Date:   Tue Mar 15 11:54:50 2022 +0100

    Improve error messages for instrument creation

    [AccuRev transaction: 20839205]

commit c4d1ebd3da59efa7223876b1de37960f2e6bcbff
Author: john.smith
Date:   Fri Mar 11 16:52:04 2022 +0100

    Added new libraries

    [AccuRev transaction: 20829020]
   ...............................

So for example I need to find the commit which message contains this string (with specific Transaction number value): [AccuRev transaction: 20829020] So two questions:

  1. how to get this specific log message from all the git logs and how to retrieve commit hash id for that particular commit?
  2. will it be enough to execute command git checkout -b branchName commitHashId to create a new branch from that specific start-point?

Edit: git log --grep does not provide me with correct result when trying to filter specific ID: Please look at the example:

git log --grep="[AccuRev transaction: 698102]"
commit f6d975e531b15c14683155a9e3ceca45d6a51854 (HEAD -> SoftBroker)
Author: stefan
Date:   Mon Feb 21 10:57:34 2022 +0100

    SPRs ,,,JIRA Issues SOF-46,SOF-49,SOF-6782,SOF-6784 Promote pom files.

    [AccuRev transaction: 20754456]

commit 0ee4ede74e3efe9d98a42ae5a6cb4c2641cd1384
Author: alek
Date:   Mon Feb 7 17:08:17 2022 +0100

    SOF-6707: Account should be pre-selected after user login    
    [AccuRev transaction: 20706246]

commit 633a0f21584f5578aaac1848255aa850bc95b52a
Author: alek
Date:   Mon Feb 7 17:06:18 2022 +0100

    JIRA Issue increasing version to 2022.1.1 and 2022.Q1.1

    [AccuRev transaction: 20706239]

Thanks a lot

vel
  • 1,000
  • 1
  • 13
  • 35

2 Answers2

2

To find a revision that has a certain message you do:

git log --grep=whatever-you-need-to-find

That should give you a list of revisions that match the regex that you provided. Then, it is the question about checking out an a branch.

git checkout some-revision-id does not create a new branch. All git will do is go to that revision and put in on the working tree and you will be working on what is called a detached HEAD.... in other words, you will be working without a branch (perfectly fine... one of the best features that git has... among a big list of great features). If you want to create a branch from that point, you should then run

git checkout -b some-new-branch some-revision-id

Which will create the new branch on that revision and check it out, in a single operation.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • `git log --grep` does not provide correct result! Please look at my Edited question. It presents many log messages which does not correspond to my desired commit hash. what is going wrong? – vel Mar 21 '22 at 18:36
  • Hmm.... you do know that `[` has a special meaning in regex, right? Just in case, try removing `[` and `]`. – eftshift0 Mar 21 '22 at 19:02
  • great!it does work I am marking the answer! If I am using the script to execute this - how I could retrieve the value of the commit ID (`commit 07254da8cd0a5c4732f13ba11103a6599b2ceced`). If I execute it manually I can copy paste it but is there some method to get it automatically (using the shell/bash) ? – vel Mar 21 '22 at 19:28
  • You can save output of commands into variables in bash like this: `some_var=$( some command ); echo $some_var` – eftshift0 Mar 21 '22 at 22:10
2

Git has revision syntax for simply naming commits by message content.

git checkout -b mynewbranch ':/\[AccuRev transaction: 20829020\]'

with the backslashes needed because [ is a syntax marker in the search- expression language

jthill
  • 55,082
  • 5
  • 77
  • 137
  • wooow! really, just like that? I am going to try it now! Also jthill can you please tell me if this creation of new branch in this way can be done if all the commits have been already pushed for the original branch, or this can be done only if `git add` and `commit` have been performed but not a `git push` as well? Thanks – vel Mar 21 '22 at 19:31
  • Yes, of course. All branch names are repo-local. – jthill Mar 21 '22 at 19:36
  • Sorry I am not a git expert - so that means they will contain `git log` messages locally even if I have pushed those messages to the remote as well? – vel Mar 21 '22 at 19:37
  • 1
    push and fetch *copy* history. Git itself is literally incredibly simple. It's so simple people can't believe it. What you can *do* with it is endless, and it comes with an endless array of tools to do those things, but Git is a dag of snapshots with labels on and that's it. A branch is a label hung on a commit. push and fetch copy commits and re-hang labels. That's it. Anything you can do with that structure, you can do with Git. – jthill Mar 21 '22 at 20:20
  • 1
    I didn't know this. So cool! Thanks for the tip, @jthill – eftshift0 Mar 21 '22 at 22:09
  • 2
    @eftshift0 when I didn't want to talk about computers I used to tell people I read manuals for a living. Seems now I read them for fun. This is disturbing. – jthill Mar 22 '22 at 00:30
  • @jthill so basically there is no difefrence if I first do execute or do not execute `git push` command before your amazing command: `git checkout -b mynewbranch ':/\[AccuRev transaction: 20829020\]'`. The final effect will be exactly the same, correct? or you do think I should have some preference when choosing if I should first do the push or not before going with `checkout`... thanks – vel Mar 22 '22 at 20:20
  • Depends on whether you want to push that ref too, but that's the only difference, push is for bringing a remote up to date with what you have, its only effect on the local repo is it updates your local refs that (by default) track that repo's branches-as-of-the-last-time-you-talked-to-it. – jthill Mar 22 '22 at 20:27
  • @jthill could I please ask you for your kind assistance on the similar issue that I am facing on the following question? Thanks a lot! https://stackoverflow.com/questions/72678821/python-and-string-literal-to-execute-command-for-git-log-filtering – vel Jun 19 '22 at 18:59