Search code examples
gitgitlabgit-resetgit-amend

What to do when force pushing is denied by remote hub?


Okay, so this is a question after the fact, and I just want to know what I should've done so I can avoid this problem in the future. Yes, I realize force pushing is a dangerous command. Yes, I realize git reset HEAD --hard can be a dangerous command too. Please help me avoid this problem that cost me ~4 hours of work today.

So here's what happened:

I had edited about 6 files on my current working branch. 3 of those files I wasn't ready to commit yet, so I staged and wrapped 3 of the files into a commit and pushed them to my remote. This left me with 3 unstaged files on my local that contained the bulk of my work.

Soon after pushing, I realized I made a minor typo in one of the files I had just pushed. So instead of adding another commit for the typo, I tried to amend my last commit and force pushed. I then received this error:

remote: GitLab: You are not allowed to force push code to a protected branch on this project.

I believe this was a recent global change made by the maintainer of our enterprise GitLab servers.

Now, this is a personal repo I am the maintainer of; hence why I had no qualms to force push. Especially when it was just a tiny change, and no one else's local git history was going to be messed up due to the push.

But now I was left with a predicament. I have this amended commit on my local machine that I would never be able to push to the remote. So I figured I needed to reset so I could just add an additional commit for the typo. So I ran:

git reset HEAD~1 --hard

And, as I'm sure you've guessed, I lost all the files that I hadn't staged for commit yet. facepalm...

What should I have done in this situation? And while I'm pretty sure those files are gone forever, is there any slight hope I could recover those changes?


Solution

  • Best would have been to stash the uncommited changed files. Something like this:

    git status
    # Shows 6 files uncommited changes
    git add file1 file2 file3
    git commit -m "Update file 1, 2, 3"
    git push origin feature-branch-123
    git status
    # Shows 3 files uncommited changes
    # work, work, work, ...
    # notice forgot something in file1
    git add file1
    git commit --amend --no-edit
    git push origin feature-branch --force-with-lease
    # returns remote: GitLab: You are not allowed to force push code to a protected branch on this project.
    
    # Now the difference:
    # before reset, stash changes
    git stash push
    git status
    # no local changes detected
    git reset HEAD~1 --hard
    # restore stashed changes
    git stash apply
    git status
    # Shows 3 files uncommited changes
    

    Stashing also works with untracked files (-u) and you can have multiple stashes if you work on different topics in parallel. Read more about stashing.

    Other than that, you could also have done a soft or a normal reset. That way you wont loose your local changes. Read more on the topic.

    If not done, I would recommand to read the git book and take a look at dangitgit for some complex troubleshooting, both free.