The git push --force -u origin
command uploads content from a local repository to a remote repository.
Normally, Git only allows changes to be pushed if the commit history of your local branch is up to date with the remote repository branch. The git push --force -u origin
command overrides this restriction in Git, meaning it allows you to forcefully overwrite the commit history of your local branch to the remote repository branch.
The process is illustrated below:
The options in the git push --force -u origin
command are described as follows:
--force
: The --force
flag is responsible for overriding the protective measures of the remote repository to ensure that your changes are forcefully pushed.
-u
: The -u
flag creates a tracking reference for every branch that you successfully push onto the remote repository. Tracking references can be used in argument-less commands, e.g., git pull.
origin
: The origin
option refers to an alias on your system for a remote repository origin
saves you the trouble of writing out the entire remote repository URL when you need to push changes.
Since the git push --force -u origin
command forcefully overwrites the commit history of the remote repository, its use is discouraged when working on shared repositories.
If someone else makes a new commit to the remote repository that is not present in your local commit history, using the git push --force -u origin
command will remove that commit. Therefore, the following safety rules should be kept in mind when using the git push --force -u origin
command:
Do not use the command on shared repositories unless you are certain your colleagues have not made any changes since your last pull
request.
Use the git revert command if you need to correct a mistake that you may have pushed onto the remote repository.
Use the --force-with-lease
flag instead of the --force
flag. If your colleagues change the remote repository after your last pull
request, this flag will raise an error to prevent you from overwriting the new commits.
For more details and variants of the
git push
command, you can check the official documentation.
Free Resources