Note that you should only ever do this if you are absolutely sure that you are the only person pushing to that branch.
You should consider using "--force-with-lease" instead of "--force". Otherwise you'll sooner or later overwrite a coworker's push without a trace. Maybe they'll notice know Git good enough to fix this, but it would still be very, very annoying.
EDIT: Changed "repository" to "branch", mention "--force-with-lease".
I would revise this to say the only person pushing to that branch. But yeah.
I've worked with multiple orgs where the convention for shared repos was to use branch names of the form <user>/<topic>, e.g. bowie/pipeline-v2. Such "user" branches should generally never be pushed to by anyone other than the dev they were named for. This has worked great to allow individuals to have visibility and/or backups of work-in-progress while avoiding lossage due to any branch cleanup prior to PR, etc.
This is the kind of thing that makes it hard to see why git has become the One True DVCS - you can not just overwrite history but actually lose other people's work quite easily (presumably the commit also vanishes from their repo when they next pull?)
Force pushing is super powerful when you learn to use git properly, but it's also like a gun with no safety so it's easy to shoot yourself in the foot if you don't know what you are doing.
After starting the git shell in the terminal (just git sh for the eponymous program) all command are simply the same as in git, only without git in front of them (so add, push, etc.). Aliases work as expected of course, so st for status, and d for diff are possible too.
These type of shells have the added benefit of a status prompt showing the basic git info, such as current branch and number of commits ahead or behind the tracked branch.
What I do have is loads of aliases for getting different logs and graphs, because all the sensible formatting options are impossible to remember. I would be lost without these. Straight from my bash profile:
# Quick summary git log variants (with and without hash, with condensed whitespace or tabs)
alias 'gl=git --no-pager log --pretty=tformat:"%ad %an: %s" --date=short -n 15'
alias 'gll=git --no-pager log --pretty=tformat:"%ad %h %an: %s" --date=short -n 15'
alias 'glxl=git --no-pager log --pretty=tformat:"%ad %H %an: %s" -n 15'
alias 'glt=git --no-pager log --pretty=tformat:"%ad%x09%an%x09%s" --date=short -n 15'
alias 'gllt=git --no-pager log --pretty=tformat:"%ad%x09%H%x09%an%x09%s" --date=short -n 15'
# Same, in reverse order, plus show slightly more entries (most recent won't scroll off top of screen)
alias 'glr=git --no-pager log --reverse --pretty=tformat:"%ad %an: %s" --date=short -n 25'
alias 'gllr=git --no-pager log --reverse --pretty=tformat:"%ad %h %an: %s" --date=short -n 25'
alias 'gltr=git --no-pager log --reverse --pretty=tformat:"%ad%x09%an%x09%s" --date=short -n 25'
alias 'glltr=git --no-pager log --reverse --pretty=tformat:"%ad%x09%H%x09%an%x09%s" --date=short -n 25'
# Quick Git Graph viewing
# Usage: gg <list of branches> | gg --all
alias 'gg=git --no-pager log --decorate --oneline --graph -n 25'
alias 'gga=gg --all'
There's a naming convention of 'gl' for 'git log' and 'gg' for 'git graph' where 'll' means "log, long" and "lxl" means "log, extra long". Adding 't' means tab-separated for piping & parsing, or copying into a spreadsheet and 'r' means reverse (which I would actually consider 'forwards' but I try to stick with git conventions even though it's the opposite of say, svn). I did not create every permutation, but all of these variants are useful quite often and if I try to run a variant that doesn't exist, I just add it quickly so it's there next time.I'm the maintainer so feel free to suggest ones.
gdh -- git diff HEAD (what have I modified since last commit)
gl -- git log --graph --oneline --all --decorate (ASCII graph of all commits on all branches)
Along with the ones provided by the git oh-my-zh plugin I have these[0].
Some examples
+ gco - git checkout
+ ga - git add
+ gc - git commit
+ gp - git push
+ gl - git pull
+ gpc - Pushes current branch to origin
+ gpcfl - Force pushes the current branch to origin using --force-with-lease
+ glc - Pulls curent branch from origin
This is my favourite though
function goops { git add -A "$@" && git commit --amend --no-edit && gpcfl }
Used like this `goops file`, it adds the file, amends the last commits and force pushes the branch. Super useful because I tend to realise I forgot something just after I push. Don't do this on shared branches though, force pushing should only happen to branches were you are the only one working on the branch or in coordination with anyone else working on the branch.
0: https://github.com/k0nserv/dotfiles/blob/master/files/zshrc....