Git get history of branches that i was in
Asked Answered
A

3

11

I need to see the history of branches that i was using. For example i have 4 branches: master, b-1, b-2, b-1-1. Thе branch "b-1-1" is child of branch "b-1". First i was at master branch, then at branch b-1, then at branch b-1-1 , then at branch b-2, then again at b-1-1. The history of my used branches would look like this:

  • b-1-1
  • b-2
  • b-1-1
  • b-1
  • master

Is it possible to do so in git? If yes, then how? I have tried to check the git log but it is not what i am searching for.

Abridgment answered 25/10, 2020 at 8:1 Comment(0)
A
2

I don't think you can see which branch were checked out and in which order in a sense that you've formulated in the question . Branch is a pointer and this pointer can change only if you do commit.

For example, if you:

  • checkout the existing branch branch (git checkout abc)
  • see its log (git log -n 10)
  • checkout another existing branch xyz (git checkout xyz)

Then git won't remember that you were checking out the abc branch

Having said that, you can see the commits that you've done during, say last 3 days with this command:

git log --since="3 days ago" --author=<HERE_COMES_YOUR_NAME_IN_GIT> --all

This --since parameter can be really flexible, 1 day ago, exact time, 1 week ago are all possible values, check out the documentation and also this SO thread

Another interesting option is using (in its the most basic form): git for-each-ref --sort=-committerdate refs/heads/

This command will print all commits in all branches in the descending order. There is already thread in SO about this and it provides way more options of possible usage of this command than I can do so please check that out as well.

Ambie answered 25/10, 2020 at 8:34 Comment(2)
c'mon. git reflog|awk '/: checkout:/{print $NF}'Menchaca
@Menchaca I was about to nitpick with "What if OP has not used git checkout but git switch? Huh? Huh?" ...then I tested it out and learned that it makes no difference in terms of reflog entries : 64ea7a0f3 (HEAD -> dev) HEAD@{0}: checkout: moving from somebranch to dev. So thank you :-)Rigney
R
16

Take a look at git reflog to inspect the history of HEAD positions.

There's also git reflog <branch> for the history of a specific branch.

Check documentation here.


(Bonus)
If useful in your context, you can also craft an alias to extract them by using the @{-<n>} construct

$ git config alias.last '!f() { for i in $(seq 1 $1); do git name-rev --name-only --exclude=refs/tags/\* @{-$i}; done; }; f'

$ git last 3
# outputs the 3 last checked out branches
Rigney answered 25/10, 2020 at 8:28 Comment(2)
Your alias suggestion is really useful, too bad it's not part of the normal git commands. I would use the alias name branchistory. If I could I would give you 50 points! You could also add to the alias << |sort|uniq >> to remove duplicates...Thrasonical
@MartinP. I did not pipe it to sort -u because the branch order could be useful also, but yes it's a possibility, it'll be better in some contexts.Rigney
B
5

This invocation of git reflog with some grep filtering seems to be working for me:

Bash

git reflog | grep checkout | grep -o 'to .*$' | grep -o ' .*$' | less

Powershell Core

git reflog | Select-String 'checkout' | ForEach-Object { $_ -replace '^.*to ', '' } | Out-Host -Paging

For example:

 b2
 master
 b1
 master
 b2
 b1
 b2
...
Blanketing answered 9/9, 2022 at 19:6 Comment(0)
A
2

I don't think you can see which branch were checked out and in which order in a sense that you've formulated in the question . Branch is a pointer and this pointer can change only if you do commit.

For example, if you:

  • checkout the existing branch branch (git checkout abc)
  • see its log (git log -n 10)
  • checkout another existing branch xyz (git checkout xyz)

Then git won't remember that you were checking out the abc branch

Having said that, you can see the commits that you've done during, say last 3 days with this command:

git log --since="3 days ago" --author=<HERE_COMES_YOUR_NAME_IN_GIT> --all

This --since parameter can be really flexible, 1 day ago, exact time, 1 week ago are all possible values, check out the documentation and also this SO thread

Another interesting option is using (in its the most basic form): git for-each-ref --sort=-committerdate refs/heads/

This command will print all commits in all branches in the descending order. There is already thread in SO about this and it provides way more options of possible usage of this command than I can do so please check that out as well.

Ambie answered 25/10, 2020 at 8:34 Comment(2)
c'mon. git reflog|awk '/: checkout:/{print $NF}'Menchaca
@Menchaca I was about to nitpick with "What if OP has not used git checkout but git switch? Huh? Huh?" ...then I tested it out and learned that it makes no difference in terms of reflog entries : 64ea7a0f3 (HEAD -> dev) HEAD@{0}: checkout: moving from somebranch to dev. So thank you :-)Rigney

© 2022 - 2024 — McMap. All rights reserved.