134

Is there a way I can keep track of commands I used in Git under Windows? I want to view all the commands that I have applied on my repository.

I want to be able to go back through the command history and figure out the command that caused the problem if one occurred.

Seeing the history of commits would be one thing, but for keeping a history of other actions such as creating a branch or adding a remote, are these tracked?

random
  • 9,774
  • 10
  • 66
  • 83
nilMoBile
  • 1,932
  • 4
  • 15
  • 20
  • 3
    This is a bit of an aside, but if you look in `.git/logs`, you'll see the history of what commit each branch pointed to. – Nayuki Sep 15 '11 at 18:12
  • 1
    @Nayuki Since `push`ing doesn't change the state of a local branch, the `reflog` apparently doesn't store `push`es, so looking in `.git/logs/refs/remotes/*/*` is the only way to see your `push` history. – Kyle Strand Feb 08 '16 at 18:07

7 Answers7

221

You can see the history with git-reflog (example here):

git reflog
Jari Keinänen
  • 1,361
  • 1
  • 21
  • 43
jweyrich
  • 31,198
  • 5
  • 66
  • 97
  • 26
    @PauAI while `git reflog ` would show all the positions that your HEAD was following, it will not actually show all the git commands. Git commands that don't move the head (for example `git branch -D my_precious_branch`) would not show up there. – Yonatan Nov 22 '16 at 08:56
  • @Yonatan, couldn't you get it with `git reflog --all`? – combinatorist Mar 28 '18 at 15:44
  • 2
    @combinatorist try adding a remote (as the OP gave for example): `git remote add foo bar`. it will not appear in `git reflog --all`, as it does not modify the HEAD pointer. – Yonatan Apr 01 '18 at 08:10
  • 1
    `git reflog` does not show me commands like `git status` aso., with or without `--all` parameter. – questionto42 Jul 07 '20 at 23:18
  • 1
    @questionto42 that's expected. `git status` only reads data - it doesn't change anything, so it cannot cause any harm or problem. Git itself doesn't record all commands executed. The reflog doc says: "record when the tips of branches and other references were updated in the local repository". A _hackish_ approach to record everything would be to replace the git binary with a shellscript (or whatever) that logs all arguments and then calls the original binary forwarding the same arguments. – jweyrich Nov 03 '21 at 15:53
81

A log of your commands may be available in your shell history.

history

If seeing the list of executed commands fly by isn't for you, export the list into a file.

history > path/to/file

You can restrict the exported dump to only show commands with "git" in them by piping it with grep

history | grep "git " > path/to/file

The history may contain lines formatted as such

518  git status -s
519  git commit -am "injects sriracha to all toppings, as required"

Using the number you can re-execute the command with an exclamation mark

$ !518
git status -s
random
  • 9,774
  • 10
  • 66
  • 83
12

Type history in your terminal. It's not technically git, but I think it is what you want.

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
10

If you are using CentOS or another Linux flavour then just do Ctrl+R at the prompt and type git.

If you keep hitting Ctrl+R this will do a reverse search through your history for commands that start with git

crmpicco
  • 16,605
  • 26
  • 134
  • 210
6

If you use Windows PowerShell, you could type "git" and the press F8. Continue to press F8 to cycle through all your git commands.

Or, if you use cygwin, you could do the same thing with ^R.

mamboking
  • 4,559
  • 23
  • 27
  • In normal windows session you can use F7 key to get a small window that gives you a list of all commands entered in that session – milso Aug 28 '19 at 08:54
1

I found out that in my version of git bash "2.24.0.windows.2" in my "home" folder under windows users (C:\Users\<yourusername>\), there will be a file called ".bash_history" with no file extension in that folder. It's only created after you exit from bash.

Here's my workflow:

  1. before exiting bash type "history >> history.txt" [ENTER]
  2. exit the bash prompt
  3. hold Win+R to open the Run command box
  4. enter shell:profile
  5. open "history.txt" to confirm that my text was added
  6. On a new line press [F5] to enter a timestamp
  7. save and close the history textfile
  8. Delete the ".bash_history" file so the next session will create a new history

If you really want points I guess you could make a batch file to do all this but this is good enough for me. Hope it helps someone.

hrdom
  • 83
  • 1
  • 7
user65795
  • 37
  • 2
  • 9
-7

git will show changes in commits that affect the index, such as git rm. It does not store a log of all git commands you execute.

However, a large number of git commands affect the index in some way, such as creating a new branch. These changes will show up in the commit history, which you can view with git log.

However, there are destructive changes that git can't track, such as git reset.

So, to answer your question, git does not store an absolute history of git commands you've executed in a repository. However, it is often possible to interpolate what command you've executed via the commit history.

Alex
  • 9,313
  • 1
  • 39
  • 44
  • 3
    Actually, as an answer below rightfully mentions, it does. "history" shows the list of commands executed (since the installation). – Umesh .A Bhat Jun 17 '15 at 15:42
  • 2
    The *shell* stores the history, but git doesn't, right? – Alex Jun 18 '15 at 07:40
  • 3
    @UmeshABhat `git history` would be useful for e.g. seeing which commands have been executed *in a particular clone*. – Kyle Strand Feb 08 '16 at 18:03
  • 1
    What's wrong with this answer that it was downvoted so much? – cp.engr Feb 10 '17 at 20:22
  • 6
    `git reflog` is what you want. See answer below. – Chéyo Nov 07 '17 at 18:51
  • 4
    @cp.engr: The OP asks for the history of git commands, not git commits. This answer suggests inferring the commands from the git commits, which is very hard work and not always possible. `git reflog` actually shows the commands themselves. Also, BTW, the shell is only a slightly more direct way to infer the git commands, but a git repo can be touched by multiple shells. It's better to ask git what commands were used. – combinatorist Mar 28 '18 at 15:42