Fixing your mistakes on GitHub vs locally
So far, we’ve looked at how to fix mistakes on the local repository. Mistakes here are a little less dangerous, because, since you're only working on your own local copy of the repo, none of your changes will affect other users. But what if you push a commit to your remote repository and realize you've made a mistake?
Once your remote repository contains something you want to fix, remember that the consequences are much higher. You stand a chance of having shared your code, and others may clone a copy with your mistake on it! 😱
If you are working with a team, before you even try to fix it, make sure to let your teammates know that you have made a mistake and not to use that version! The last thing you need is for your team to work on projects with your commit that has all the wrong files in it! Oops! 😬
Yikes! Get your bad commit out of GitHub, pronto!
Now if you have pushed your branch to your own account, and it hasn’t merged with the project yet, you can still make your fixes. Phew! 😅
As you might have noticed in the last chapter, every commit has an identifying character string associated with it. So, you need to figure out the string associated with the commit you want to move, and backtrack it. This unique identifier is called a hash:
For example, if your mistaken commit was 123456, you would type in this super safe command:
git revert 123456
Then you can do one of two things:
Option 1: Use
git push origin +master. The
+ sign forces the push to the remote repository.
Option 2: Delete a specific commit using git push. The command is
git push <repo name> +<badcommithash>^ : <branch>. Let’s say that the name of your repo is myrepo, and the hash for the commit you want to delete is 123456. Your command will look like this:
git push myrepo +123456^:master
You can also delete an entire branch using
git push <repo name> :<branch name>.
It's important to note that this push will make the required changes for your pull request and a direct push to a GitHub repository. If you use this command directly on the master branch, the change will also show in the commit history.
Remote access not working?
Now let’s take a look at some other issues that may happen while you’re trying to connect your local repo to your remote repo on GitHub.
Let’s say that when you enter this command -
git push origin master - you get a "Permission denied" error code indicating that you haven’t been able to connect or authenticate to your remote repository. 😰
Don't worry. This is a common problem, and is usually due to having the wrong credentials stored to your account. It can happen if you have multiple accounts, or switch to another GitHub account.
On Windows, you can go to Control Panel -> User Accounts -> Manage your credentials -> Windows Credentials. Then click Remove:
Now when you try to run git push origin wwbranch again, you will get the GitHub login screen, and you'll just have to log into your account. Easy peezy! 😎
That's it! You should see the remote connection validated at this point, and your code should push to GitHub.
Git version issues with SSH
Sometimes you don’t get time to update your version of Git that you use on your local computer. Then Git Bash won’t let you push your latest changes to the remote repository because of the OpenSSH and OpenSSL version. 😡
Don’t worry, this is a safeguard to keep you from losing your data to an attacker.
You see, OpenSSH and OpenSSL versions are important to keep updated, so you are working with the latest security updates on Git Bash. All you have to do is reinstall Git with the latest security updates and SSH version.
You can do that by getting the latest version of Git on the Git website and downloading it. When you install, it will upgrade your version so you can go back and push that local repo to GitHub.
Fixing mistakes in the remote repository requires more damage control.
If you push a mistake onto GitHub, be sure to alert team members so they do not accidentally clone the mistake.
git push <repo name> + <hash>^:<branch>will delete a specific commit that you may have put in a pull request, either on your forked copy, or pushed directly to GitHub.
Your GitHub account credentials are saved on your computer and must be deleted if you plan to use a different account.
GitHub will not connect remotely if you don't have the latest OpenSSL version. You can fix that by reinstalling the latest version of Git.