Tuesday, May 7, 2019

shell function to put changes in previous git commit

code for '~/.profile' file:

amendPrevCommit() {
  BRANCH=`git branch | grep \* | cut -d ' ' -f2`
  cd `git rev-parse --show-toplevel`
  git stash
  git branch -D $BRANCH-temp-for-stash
  git checkout -b $BRANCH-temp-for-stash
  git checkout -
  git reset HEAD~1 --hard
  git stash pop
  git add .
  git commit --amend --no-edit
  git cherry-pick $BRANCH-temp-for-stash
}

after update of ~/.profile
do not forget to do "source ~/.profile" in terminal window

Example of use:
$ amendPrevCommit