Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

git

What is the git push --force -u origin command?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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:

Remote Repository before and after the `git push --force -u origin` command

Options

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 URLUniform Resource Locator. origin saves you the trouble of writing out the entire remote repository URL when you need to push changes.

Safety rules

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.

RELATED TAGS

git

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring