Use Git Push -Force Carefully

I was trying to push a change to a commit that had not been made into a pull request yet. My commit message didn’t propagate to the pull request. (My sleepy brain forgot that commits on top of unpulled commits don’t propagate to the title of the pull request.)
I thought I’d done something wrong and tried something that seemed mostly harmless (as the aliens say about earth in Hitchhiker’s Guide).
It didn’t work, but it didn’t seem to have changed anything, so I shrugged and moved on, finally learning the above wisdom.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git commit –amend
kat257-simpleanalyzerupdate 58b0cc6b9ac DOCS Modified section headings, edited text for clarity.
Date: Mon Jul 29 20:10:05 2019 -0400
1 file changed, 1 insertion(+), 1 deletion(-)
Back the next morning, I made another little commit.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git commit docs/reference/analysis/analyzers/simple-analyzer.asciidoc
kat257-simpleanalyzerupdate 820fc51c538 DOCS Modified section headings, edited text for clarity.
1 file changed, 2 insertions(+), 2 deletions(-)
…
Bad news. I couldn’t push to my fork.
Two hours of headbanging ensued. It was too embarrassing to ask for help.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git push
To github.com:kat257/elasticsearch.git
! [rejected] kat257-simpleanalyzerupdate -> kat257-simpleanalyzerupdate (non-fast-forward)
error: failed to push some refs to ‘git@github.com:kat257/elasticsearch.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
I tried to use git pull to straighten my fork out.
The pull went fine, but did not solve my problem.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git pull
…
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git push
To github.com:kat257/elasticsearch.git
! [rejected]
…
Here I tried git pull –rebase origin master.
Not the solution.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git pull –rebase origin master
From github.com:elastic/elasticsearch
- branch master -> FETCH_HEAD
First, rewinding head to replay your work on top of it…
Applying: DOCS Modified section headings, edited text for clarity.
Applying: DOCS Modified section headings, edited text for clarity.
Applying: DOCS Modified section headings, edited text for clarity.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git push
To github.com:kat257/elasticsearch.git
! [rejected]
…
By now I’m just flailing.
I switch to master and git pull there, then switch back and commit. Didn’t need to do any of this.
MKs-MacBook-Pro:elasticsearchmaster$ git pull origin master
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git commit docs/reference/analysis/analyzers/simple-analyzer.asciidoc
On branch kat257-simpleanalyzerupdate
nothing to commit, working tree clean
So I try again, to no avail.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git push
To github.com:kat257/elasticsearch.git
! [rejected]
…
Repeat for good luck?
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git pull origin master
I learn a new, dangerous thing, git push –force, so I screw it up trying to be extra careful, as if my bash instructions to push to my fork are going to poof out of existence. Notice that I’m pushing to master on my fork? Yeah, not gonna work.
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$ git push kat257 master –force
…
After a tad more ass-covering, I finally get (git) it right.
A new commit to my pull request. yay.
MKs-MacBook-Pro:elasticsearch[kat257-simpleanalyzerupdate]$ git push –force
Enumerating objects: 49, done.
Counting objects: 100% (49/49), done.
Delta compression using up to 12 threads
Compressing objects: 100% (32/32), done.
Writing objects: 100% (34/34), 3.72 KiB | 1.86 MiB/s, done.
Total 34 (delta 24), reused 0 (delta 0)
remote: Resolving deltas: 100% (24/24), completed with 10 local objects.
To github.com:kat257/elasticsearch.git
- 3cbd888a458…bffaa19eca9 kat257-simpleanalyzerupdate -> kat257-simpleanalyzerupdate (forced update)
MKs-MacBook-Pro:elasticsearchkat257-simpleanalyzerupdate$