Practical Git via GitExtensions
  • Git - a Distributed System
  • Bare Minimal Configurations
  • Creating a New Branch
  • Making a Commit
    • Adding only a portion of a file
    • Commit Message
    • Cherry-pick
    • Revert
  • Rewriting History
    • Rewording the Last Commit
    • Rewording Commit Message in Middle
    • Editing the Last Commit
    • Squashing Last Few Commits
    • Squashing Commits in Middle
    • Spliting Commit into Multiple
    • Removing a Commit
    • Pushing after Rewriting History
  • Lost and Found
  • Comparing Commits or Branches
  • Checking out a Branch into Another Folder
  • Blaming History
    • Blame
    • Bisect
  • Disabling Auto Tracking Branch
  • Line Ending
  • Ignore Unwanted Files
Powered by GitBook
On this page

Was this helpful?

  1. Rewriting History

Pushing after Rewriting History

PreviousRemoving a CommitNextLost and Found

Last updated 6 years ago

Was this helpful?

If we've previously pushed the branch, the server will reject our push after rewriting history, which can be forced with --force-with-lease.

Important:

Before using the commandline version of force push such as git push --force-with-lease or git push --force, you should make sure the push default setting is not match. When it's match git will push all your local branches that matches the branches on the remote server thus extremely dangerous when combined with force

# to get the value from the global settings
# assuming it.s global as very likely you won't configure it per project
git config --global --get push.default
# to set the value to nothing
git config --global push.default nothing

What is the difference between force-with-lease and force push

Force with lease is a little bit safer than force as it checks if the remote branch at local PC is the same as the branch at the remote server, for example, the push of awesome_branch to GitHub (let's say we are using GitHub) will fail if the local origin/awesome_branch at our side is different to the awesome_branch on GitHub (Someone else may have pushed something to the branch, but we haven't fetch the commits).

See for other options

https://git-scm.com/docs/git-config#git-config-pushdefault