2

It's possible to git merge even if current branch has uncommitted changes. Example:

git checkout featureA
ls >> foo
git commit -am "added featureA"
git checkout dev
ls >> bar                    # current branch now has uncommitted changes
git merge featureA

I wonder if it's possible to configure git to always fail as long as current branch has uncommitted changes, just to make the merge safer.

wlnirvana
  • 1,811
  • 20
  • 36
  • Add them to the index? Otherwise, I'm not sure how this is unsafe. Git will avoid merging if things are unsafe (see also "Pre-merge Checks": https://git-scm.com/docs/git-merge#_pre_merge_checks). – Whymarrh Feb 22 '16 at 02:03

1 Answers1

0

My first thought was to declare a Git alias to override the merge command and check the state of your working-directory and index prior to performing the operation.

Turns out it's not possible to overwrite an exiting Git command with a Git alias (see this question for more info) but you could define a shell-level alias.

To keep things simple, perhaps it would be better to define an alias called safemerge. You could also define an additional alias to make detecting a clean state easy.

Define these as aliases in your .gitconfig:

isclean = !(git status --porcelain | grep . && exit 1 || exit 0)
safemerge = !git isclean && git-merge

Now, when you go to merge you can run git safemerge and the merge will not happen if you have uncommitted changes.

Community
  • 1
  • 1
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115