7

Git has --force flag in a lot of operations but when I use them I feel a bit strange. Like ignoring warning messages in my code.

For example I just wanted to unstage a file and did this:

git rm --cached myfile.ext

git complained error:

the following file has staged content different from both the file and the HEAD

I really don't care about this error and it seems not to be an issue - I just want to leave my code as it is, but unstage the file. --force flag simply solved this issue.

My question is about best practices as I was unable to find any information about this issue - should one use force flags in git commands or is it a bad practice?

UPDATE

I have found this question of mine and had some insight that my be useful to anyone interested.

force flag is like kinda yes/no dialog in git(or any similar software). Like when doing something that might fail or damage something usually we get yes/no dialog. So git equivalent of yes is --force which answers my question that force flag usage is just a normal routine.

lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81

2 Answers2

6

--force does not mean to take a chance and try something that will likely go wrong, but to enforce an action being aware of possible conflicts. So I agree that using --force is not a bad practice.

You often want exactly that: overwrite some check that the git command has in place to prevent you from doing something stupid. If you take notice of the warnings and know that the command will succeed, there is nothing bad about using force. See the docs (or git's help pages on the command line) to see what is overwritten for each command.

Another use case is to avoid confirmation prompts when running a command from a script where you do not want or can not handle user interaction.

Wolfram
  • 8,044
  • 3
  • 45
  • 66
3

First, to unstage, you would do a git reset -- file.

Second, sure using --force isn't a bad practice.
One common example is when you want to add to source control a file which is part of ignored files (declared in a .gitignore)

git add --force -- aFile

Don't forget that some of those same command (like git add) have a "preview" or "dry-run" mode (-n option). Another good practice is to test the command with that -n option.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I don't want to remove any uncommited code(git reset) I just want to unstage the file :). I just added new file, wrote code but don't want it in my next commit. So I want to unstage and add it later – lukas.pukenis Sep 30 '13 at 09:24
  • @lukas.pukenis that doesn't remove anything. That doesn't change your file. It only removed it from the index. – VonC Sep 30 '13 at 09:25
  • just realized that git reset --hard removes code! Thanks! :) and +1 for the -n flag. Didn't know that too. – lukas.pukenis Sep 30 '13 at 09:28
  • @lukas.pukenis for more on the difference between `git reset` and `git rm --cached` for unstaging (without deleting any file): http://stackoverflow.com/a/6049090/6309 – VonC Sep 30 '13 at 09:29