91

I am using Git for Windows (ver. 1.7.8-preview20111206) and even though I have a .bash_history file in my HOME folder, it never automatically gets updated. When I start Git Bash, I can see in the history commands that I manually added to the .bash_history file, but it does not get updated automatically.

I used the shopt -s histappend command to make sure that the history gets saved every time I close the shell, but it does not work.

If I manually use the history -w command, then my file gets updated, but I would want to understand why the shopt command does not work as I understand it should.

Anyone can tell me why is this behavior happening?

2
  • What are the values of $HISTSIZE, $HISTFILESIZE and $HISTFILE? May 7, 2012 at 21:28
  • $HISTSIZE is 10000, $HISTFILESIZE is 10000 and $HISTFILE is /h/.bash_history.
    – franmon
    May 7, 2012 at 22:39

10 Answers 10

124

I put this in my ~/.bash_profile

PROMPT_COMMAND='history -a'
5
  • 7
    To avoid the line ending problem, you can just type echo "PROMT_COMMAND='history -a'" >> ~/.bash_profile in Git bash to add the row to the end of your .bash_profile file (or to create a new one if one doesn't exist).
    – Godsmith
    May 18, 2016 at 13:24
  • 6
    @Godsmith You missed a 'P' in your command : PROM*P*T_COMMAND=... Dec 27, 2016 at 7:16
  • 3
    This also works for me on Win10. The above code makes bash append to history file on every command, as per answer in AskUbuntu. Jul 2, 2017 at 10:57
  • 10
    This worked for running git's bash in Visual Studio Code, but I had to put it ~/.bashrc not ~/.bash_profile.
    – C.M.
    Jul 29, 2019 at 14:43
  • 1
    I prefer option -n instead of -a : ss64.com/bash/history.html
    – gawkface
    Dec 21, 2021 at 15:50
44

Create the following files

~/.bash_profile
~/.bashrc

And put the following line in both of them

PROMPT_COMMAND='history -a'

To do this from the console (Git Bash) itself, use the following commands:

echo "PROMPT_COMMAND='history -a'" >> ~/.bash_profile
echo "PROMPT_COMMAND='history -a'" >> ~/.bashrc

What history -a means

From the history --help command

-a append history lines from this session to the history file

What is PROMPT_COMMAND?

Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

Difference between .bash_profile and .bashrc

.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

But, if you’ve already logged into your machine and open a new terminal window (xterm) then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

On OS X, Terminal by default runs a login shell every time, so this is a little different to most other systems, but you can configure that in the preferences.

References

1
  • 3
    Thank you for the detailed explanation how it works and why it is so and for information about difference between .bash_profile and .bashrc configuration files.
    – Alex Marty
    Aug 17, 2021 at 18:20
40

As it was said here, to save git bash history on Windows you must not close the terminal with X button. Use exit command instead. History of commands will be saved then regardless of configuration mentioned in the accepted answer.

5
  • 19
    that's good to know, but I'm going to have a hard time remembering that.
    – Chance
    Mar 16, 2015 at 14:25
  • 2
    @Chance luckily you no longer have to remember this, because in the newer versions of Git for windows this is not true. Tested with version 2.9.3 on win7 and win10.
    – Superole
    Aug 29, 2016 at 8:04
  • 4
    @Superole not true on my machine. I have git 2.12.2 for windows 8.1 (newest for 04/17), and the only way to save history i am aware of is to type "exit" before clicking x button
    – Sebastian
    May 2, 2017 at 13:36
  • 2
    Which regrettably also occurs when Windows updates apply and reboot.
    – dbreaux
    Feb 14, 2019 at 14:25
  • 7
    You can also press Ctrl+D instead of typing exit, it's much faster and simpler to use. It's even faster than using a Close button!
    – EvgenKo423
    Mar 12, 2021 at 18:48
10

If you're using Git bash in VSCode please see C.M.'s comment above.

This worked for running git's bash in Visual Studio Code, but I had to put it ~/.bashrc not ~/.bash_profile. – C.M. Jul 29 at 14:43

This solved it for me.

8

There is a more complete answer on Unix Stackexchange, by Pablo R. and LinuxSecurityFreak. Add the following to your ~/.bashrc:

# Avoid duplicates
HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Please be careful, though:

The problem with this PROMPT_COMMAND solution is that the numbers for each history item changes after each command. For example, if you type history(1) ls (2) rm, then you do !1 to repeat (1), the history number might change and might run the rm command. Chris Kimpton

0
5

If you use git bash for windows 8, just put this in your ~/.bash_logout file:

history > .bash_history

Obviously you need a ~/.bash_history file.

Regards.

5
  • Windows 8.1 and GNU bash, version 3.1.0(1)-release (i686-pc-msys) - works!
    – pbaranski
    Jul 15, 2014 at 8:36
  • 2
    @CamiloMartin - see my comment above about making sure you've used Unix line endings (I used .bash_profile).
    – psdie
    Jan 8, 2015 at 16:10
  • 2
    @psdie Thanks for the heads-up! EOLs, those bastards. Jan 8, 2015 at 19:24
  • 4
    Not working for me. As @fracz pointed out above, the issue is that Git bash does not save the history if you close the window with the X in the top right corner.
    – Godsmith
    May 18, 2016 at 13:26
  • 1
    This answer did not work for me because history includes the command numbers. Adding history -w to the ~/.bash_logout file did work since that writes the history without numbers
    – harleybl
    Aug 6, 2021 at 13:19
1

For me what worked was going into C:\Users\MY_USER\ and deleting the .bash_profile file.

Ps: I am using windows 10

1

I am using Windows 10 for me it was a permission problem, my temporary solution was to add Everyone group and give it Full control on ~/.bash_history file.

0

By the way, for those using the Portable version of Git for Windows, there's no need to create .bash_profile or .bashrc. Simply add to C:<path to your Git Portable folder>\etc\bash.bashrc:

PROMPT_COMMAND='history -a'
0

Found an answer in another post : https://superuser.com/questions/555310/bash-save-history-without-exit

If you want to have an history updated between two terminals.

As a window user I created a file .bash_profile inside my user folder. And then I add the following content : PROMPT_COMMAND='history -a;history -c;history -r'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.