Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.bash_history does not update in Git for Windows (git bash)

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?

like image 222
franmon Avatar asked May 07 '12 20:05

franmon


People also ask

Is git CMD deprecated?

Please note: Using Git from Command Prompt (cmd.exe, Git CMD) is deprecated as of this Git for Windows version. The default is to have git. exein the PATH, so there is no noticeable difference between cmd.exe and using the Git CMD start menu shortcut.

How do I save my git bash history?

bash_history? You only have to do it once though. The next time you close the window it will save. Or CTRL-D for fast exit also working.

Is Git Bash installed with Git?

Git Bash comes included as part of the Git For Windows package. Download and install Git For Windows like other Windows applications. Once downloaded find the included .exe file and open to execute Git Bash.

How to save Git Bash history on Windows?

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. Show activity on this post. To do this from the console (git bash) itself use the following commands

How to update to the latest version of Git and Bash?

Note that it is technically different from Bash. On my same machine when I run: To update to the latest version of Git and Git Bash, you can download and install the latest version of Git for Windows. As per this FAQ, settings/customizations should be preserved if they were installed in the appropriate configuration folders.

How do I find the history of a Git project?

The command shown is ls (history from git bash instead of git bash from vs code session). Or history of the commands to be remembered per project/path. All the history is managed by the shell, in this case git bash. If you launch a single shell and exit it, the history will likely be retained.

What is Git Bash and how to use it?

Put simply, Git Bash is an application for Microsoft Windows OS environments that provides Unix based shell utilities and experience for Git command line commands. Git Bash emulates the Git command line experience that Unix environments have, for Windows users. Most Windows users download Git Bash when they first install Git for Windows.


3 Answers

I put this in my ~/.bash_profile

PROMPT_COMMAND='history -a'
like image 123
Zombo Avatar answered Oct 06 '22 11:10

Zombo


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.

like image 37
fracz Avatar answered Oct 06 '22 10:10

fracz


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

  • https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
  • https://apple.stackexchange.com/questions/51036/what-is-the-difference-between-bash-profile-and-bashrc
like image 42
Anddo Avatar answered Oct 06 '22 10:10

Anddo