Git get history of branches that i was in

7.1k views Asked by At

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.

3

There are 3 answers

2
Mark Bramnik On BEST ANSWER

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.

2
Romain Valeri On

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
0
NHDaly On

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

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

For example:

 b2
 master
 b1
 master
 b2
 b1
 b2
...