How To Fix A Stuck GitHub Push
Tags: git, github, linux, programming
Published: 2022-08-29 12:20:00

- [Introduction](#introduction)
- [Diagnosing the Issue](#diagnosing-the-issue)
- [Cause of the Issue](#cause-of-the-issue)
- [Solution](#solution)
    - [Step 1. Checking the Commit Log](#step-1-checking-the-commit-log)
    - [Step 2. Reset the Commit to the Last Pushed Commit](#step-2-reset-the-commit-to-the-last-pushed-commit)
    - [Step 3. Add the Files](#step-3-add-the-files)
    - [Step 4. Commit the Files](#step-4-commit-the-files)
    - [Step 5. Push the Files](#step-5-push-the-files)
    - [Step 6. Rinse and Repeat](#step-6-rinse-and-repeat)
- [Conclusion](#conclusion)
- [References](#references)

## Introduction

Recently I had an issue where a commit I was pushing to GitHub just seemed to be stuck in VS Code. It would just show the progress bar going from left to right, with the timer icon showing on the source control icon.

## Diagnosing the Issue

Using various tools, I couldn't find a reason for the issue occurring. The output for the push in VS Code showed nothing and the `git log` command just showed the commit as being there but as the head hadn't moved, it wasn't pushed to GitHub. Also, checking the commit history on the GitHub repository in a browser showed no sign of the commit.

I attempted to close VS Code, reopen it and then perform the push again thinking it might have just gotten stuck in the aether of the interwebs but it happened again.

Switching to the terminal (not the one in VS Code) and doing a `git push` from there resulted in the same behavior.

Nothing was showing as being at fault. Downdetector showed GitHub had no reported issues and it wasn't VS Code's source control extension at fault.

## Cause of the Issue

So, what was the issue and what was the solution?

I narrowed the issue down to it being too many files, specifically images, being pushed at any one time. I've done commits and pushes with many source code files before but not with a lot of images.

Now, these image (PNG) files were not huge in size (50KB - 150KB) so way below the maximum size for GitHub but they just would not push.

## Solution

The solution I came up with is as follows.

*Note: Although this is targeted at GitHub, there is no reason why it wouldn't work for other git-based repository providers, such as AWS CodeCommit, Azure DevOps or GitLab to name just a few.*

### Step 1. Checking the Commit Log

*Disclaimer: If you have any commits pending a push, they will be lost.*

*Also, please make a backup copy of your files prior to starting this. Better safe than sorry as they say.*

With that being said, first open up a terminal using the app of your choice. Next, `cd` to the directory where your git repository is on your local machine.

Next, run:

``` console
git log
```

The output will be a list of commits, similar to the below.

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk5zqrhs2yj947qbumcx.png)

### Step 2. Reset the Commit to the Last Pushed Commit

Now that you can see the list of commits, the next step is to reset the commit head to a last-know good commit. Typically, the last-know good commit will have (origin/main) next to a commit ID. You need to highlight and copy the number. An example of a commit ID is shown in the below image, highlighted in blue.

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99clxje3nz1n4egevfvt.png)

Once you have that number copied, you can now run the reset command to take the local repository back to that commit which will then clear out any commits that were made after it.

``` console
git reset commit-id
```

An example is shown below.

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwvv243lbrdfirmfdrfb.png)

Once the reset has been done, the commits after that commit ID will disappear. For example, the below image shows that the commit for "Added images and notebook" is now removed.

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3h0x5qyjwr1qidsgkfz.png)

The commit that was causing the problem will still be visible in the local commit list when you run `git reflog` but when you push the commit, those commits will not be pushed and are effectively lost as they will not show on the GitHub history.

### Step 3. Add the Files

Now that the commit that was causing the issue has been cleared, it is now time to try committing and pushing again. This is where the fix for the issue is.

The solution is to break up each commit into smaller batches of around five files, push that commit and then move onto the next one. When moving onto the next one, you can try to increase the number of files each time until the issue occurs again. At that point, just go back to step one and carry on with a smaller number of files.

First, let's take a look at the list of files that are not committed / tracked.

``` console
git status
```

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45r3vc8qrypvsz84899i.png)

The list of files that are not committed / tracked are listed in red text.

Now that we know which files need to be committed, the next step is to add five of those files to be tracked and added to the next commit. To do this, use the *git add* command. For example:

``` console
git add assets/images/2-2-1.png assets/images/2-3-1.png assets/images/2-4-1.png assets/images/3-1-1.png assets/images/3-2-1.png

```

### Step 4. Commit the Files

Now that the files have been added, the next step is to create a commit. There will be nothing different to do here that you normally would do.

``` console
git commit -m "Change message to something suitable"
```

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8biryhpcyx45ca9nb6m.png)

What you could do is use the same commit message for each commit or use a different one for each batch.

### Step 5. Push the Files

Lastly, perform a push of the commit to GitHub as normal.

``` console
git push
```

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq5va3zb416bi8wunbmr.png)

If you then run `git log` again, the origin/main should now be next to HEAD -> main (or the name of the branch you are using). For example:

![Output from command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mg5u88coljkc59pgld8.png)

### Step 6. Rinse and Repeat

Once the commit has been pushed successfully to GitHub, repeat steps two to five until all of the files have been pushed to GitHub.

## Conclusion

This was a strange issue that has occurred for me a couple of times and each time I managed to resolve the stuck push with the above solution.

If you have any other ways to resolve this issue, please feel free to leave your suggestion(s) in the comments.

Thank you for reading and have a nice day!

## References

[Git reset documentation](https://git-scm.com/docs/git-reset)

[Git reset of local commit](https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-a-git-reset-and-push-to-remote-works-on-previous-local-commits)