{"pageProps":{"data":{"title":"Force push responsibly","date":"2020-05-12T06:00:00.000Z","description":"Don't be afraid to force push. You just need to learn how to do it the responsibly.","slug":"force-push-responsibly","tags":"Today I Learned, git","locale":"en-US","dateFormatted":"Tuesday, May 12, 2020"},"content":{"compiledSource":"\"use strict\";\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar layoutProps = {};\nvar MDXLayout = \"wrapper\";\n\nfunction MDXContent(_ref) {\n var components = _ref.components,\n props = _objectWithoutProperties(_ref, [\"components\"]);\n\n return mdx(MDXLayout, _extends({}, layoutProps, props, {\n components: components,\n mdxType: \"MDXLayout\"\n }), mdx(\"p\", null, \"Force pushing is not bad or wrong. It's part of the every day workflow of developers. But it must be used with caution. A force push gone wrong can overwrite existing work. Fortunately, \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"git\"), \" provides us with an option to force push responsibly.\"), mdx(\"pre\", {\n \"className\": \"language-shell\"\n }, mdx(\"code\", _extends({\n parentName: \"pre\"\n }, {\n \"className\": \"language-shell\"\n }), mdx(\"span\", _extends({\n parentName: \"code\"\n }, {\n \"className\": \"token function\"\n }), \"git\"), \" push --force-with-lease\\n\")), mdx(\"p\", null, \"Like \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"git push --force\"), \", \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"--force-with-lease\"), \" will overwrite commits and git history. However, \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"--force-with-lease\"), \" will not overwrite any new commits a developer didn't hadn't pulled before.\"), mdx(\"p\", null, \"Imagine you are about to use \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"--force-with-lease\"), \" to a branch. If another developer committed new work to that branch that you hadn't pulled, an attempt to \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"git push --force-with-lease\"), \" that branch will fail.\"), mdx(\"h2\", null, \"Always force push with lease\"), mdx(\"p\", null, \"A good way of avoiding git havoc is by always force pushing with \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"--force-with-lease\"), \". An easy and convenient way to do this is with a \", mdx(\"a\", _extends({\n parentName: \"p\"\n }, {\n \"href\": \"https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases\"\n }), \"git alias\"), \".\"), mdx(\"p\", null, \"My alias is \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"git pushf\"), \". I added it to my global git config with.\"), mdx(\"pre\", {\n \"className\": \"language-shell\"\n }, mdx(\"code\", _extends({\n parentName: \"pre\"\n }, {\n \"className\": \"language-shell\"\n }), mdx(\"span\", _extends({\n parentName: \"code\"\n }, {\n \"className\": \"token function\"\n }), \"git\"), \" config --global alias.pushf \", mdx(\"span\", _extends({\n parentName: \"code\"\n }, {\n \"className\": \"token string\"\n }), \"'push --force-with-lease'\"), \"\\n\")), mdx(\"p\", null, \"With that alias in place, you can use \", mdx(\"inlineCode\", {\n parentName: \"p\"\n }, \"git pushf\"), \" to force push your work responsibly. Your coworkers and yourself will thank you for it.\"));\n}\n\n;\nMDXContent.isMDXComponent = true;","renderedOutput":"
Force pushing is not bad or wrong. It's part of the every day workflow of developers. But it must be used with caution. A force push gone wrong can overwrite existing work. Fortunately, git
provides us with an option to force push responsibly.
git push --force-with-lease\n
Like git push --force
, --force-with-lease
will overwrite commits and git history. However, --force-with-lease
will not overwrite any new commits a developer didn't hadn't pulled before.
Imagine you are about to use --force-with-lease
to a branch. If another developer committed new work to that branch that you hadn't pulled, an attempt to git push --force-with-lease
that branch will fail.
A good way of avoiding git havoc is by always force pushing with --force-with-lease
. An easy and convenient way to do this is with a git alias.
My alias is git pushf
. I added it to my global git config with.
git config --global alias.pushf 'push --force-with-lease'\n
With that alias in place, you can use git pushf
to force push your work responsibly. Your coworkers and yourself will thank you for it.