Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require confirmation for git reset --hard?

Tags:

git

bash

How to I make sure that git asks for confirmation on any hard reset?

I have had this several times. The terminal bugged out(bash history navigation) and showed me only the last part of the command... I pressed enter and to my surprise reset all my changes.

So is there a way to make sure that under normal operation git asks for a confirmation when performing a hard reset?

like image 436
Aleksandr Panzin Avatar asked Oct 04 '13 11:10

Aleksandr Panzin


People also ask

How do you commit a hard reset?

In order to hard reset to the commit right before HEAD, use “git reset” with the “–hard” option and specify HEAD^. As you can see, the HEAD of the release branch is now pointing to the second commit : we essentially have reset to the commit before HEAD.

How does git reset hard work?

Using the git reset --hard option resets the current branch tip, and also deletes any changes in the working directory and staging area. Therefore, it resets index entries to the state of the specified commit.

How do you get back commit after reset hard?

Hard reset explained. In this case, you can restore the file using either git checkout or git reflog . You can find the hash-ID of the previous commit from the command: git log . In case you don't have the hash ID, you can use the command git reflog .

Does git reset hard remove staged changes?

Git Reset A Specific File The changes it contains will still be present in the working directory. The --soft , --mixed , and --hard flags do not have any effect on the file-level version of git reset , as the staged snapshot is always updated, and the working directory is never updated.


1 Answers

Git itself doesn't implement this (for good!), and it's impossible to alias a built-in Git command, so I see two ways:

  • A custom wrapper — something like

     # cat >/usr/local/bin/mygit
     #!/bin/sh
     set -e -u
     if [ $# -ge 2 ]; then
         if [ "x$1" = "xreset" -a "x$2" = "x--hard" ]; then
             echo 'Sure?'
             read resp || exit $?
             test "x$resp" = "xyes" || exit 0
         fi
     fi
     git "$@"
     ^D
     # chmod +x $_
     $ mygit reset --hard
    
  • Shell aliases:

    cat >>~/.bashrc
    alias git /usr/local/bin/mygit
    ^D
    

    Where /usr/local/bin/mygit is taken from the previous point.

Update to incorporate the suggestion of William Pursell — a shell functon which "overrides" the external command:

# cat >>~/.bashrc
git() {
    set -e -u
    if [ $# -ge 2 ]; then
        if [ "x$1" = "xreset" -a "x$2" = "x--hard" ]; then
            echo 'Sure?'
            read resp || return $?
            test "x$resp" = "xyes" || return 0
        fi
    fi
    command git "$@"
}
^D

After that, plain git blah ... in a shell would call the wrapper function while direct /usr/bin/git would call Git directly.

like image 84
kostix Avatar answered Oct 13 '22 00:10

kostix