Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: should force ever be used?

Tags:

git

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.

like image 666
lukas.pukenis Avatar asked Sep 30 '13 09:09

lukas.pukenis


People also ask

Should I use git push force?

You can use the --force flag (or -f for short). This can look like an easy workaround when the git push command does not work, but it is rarely recommended — it's not the default behavior for a reason. In the scenario above, if you force push your work, you will erase Joe's commit from the remote repo.

Why force push is needed?

The most common reason is when you pushed sensitive data to the remote repository. But if someone else pulled before you force pushed, he will still have access to your private data.

What is the problem with git force -- push?

The Risks of Git Push ForceBecause you have failed to pull those changes, they are not reflected in your local repository. In this case, if you perform a Git push force, you will replace the remote repository with a copy of your local repo, effectively deleting your team member's work.

Does force push delete history?

Force pushing rewrites the repository history, which removes sensitive data from the commit history. If you force push, it may overwrite commits that other people have based their work on.


2 Answers

--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.

like image 83
Wolfram Avatar answered Sep 28 '22 15:09

Wolfram


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.

like image 29
VonC Avatar answered Sep 28 '22 14:09

VonC