Correct Your Mistakes Locally and Remotely
You’ve had a hard day, and you’ve accidentally pushed the wrong files.
The problem here is that this mistake is also going to affect the other people working on the project.
What’s the first thing to do in this case?
Let your colleagues know, of course! But luckily, Git has a solution for your problem.
You can undo a public commit using the command git revert. The revert operation cancels a commit by creating a new commit. It’s a safe way of undoing changes, as there’s no risk of rewriting the commit history.
git revert HEAD^
We’ve now reverted our last public commit and created a new revert commit. This command, therefore, has no impact on the history. This is why it’s best to use git revert
to undo changes made to a public branch and git reset
to do the same on a private branch.
Remote Access Isn’t Working
If remote access isn’t working, this could be due to an authentication problem with your network. To resolve this, you’ll need to generate an SSH key pair.
Watch the video below to make sure you don’t miss any stages, and follow the instructions below:
Let’s generate an SSH key pair.
In Git Bash, run the command:
$ ssh-keygen -t rsa -b 4096 -C "johndoe@example.com"
You’ll get this:
You can either hit Enter, or type in a file name. You’ll then be asked to provide a password.
Congratulations! You’ve generated your SSH key!
To find it, just go to this address: C:\Users\YourUserName\, and show hidden folders.
If you’re using MacOS, you can display it by:
Opening the folder in the Finder
Hold down the Command, Shift and Period keys: cmd + shift + [.]
The hidden files and folders on your Mac will then appear partially transparent
You can hide the files again using the same keyboard shortcut.
In this folder, you have two files—your public key and your private key.
The key id_rsa.txt (on Windows) or id_rsa (without an extension if on MacOS or Linux) is private and the key id_rsa.pub is public. Here, we’re only going to use the public key. You can copy your public key by opening it in a notepad in Windows, or TextEdit in MacOS.
Change Your Login Information and Delete the Key
Now let’s add the key to your GitHub account.
Sign in to your GitHub account, then go to the top right-hand corner and click on Settings.
Click on SSH and GPG keys:
Then on New SSH Key:
Choose a title and paste your SSH key:
You’ll then be asked to confirm your password, and your SSH key will be added to your GitHub account!
Let’s Recap!
Using git revert HEAD^, you can undo a commit by creating a new commit.
The command ssh-keygen generates an SSH key pair.
You can configure a new SSH key in GitHub.
Next, you’ll learn to use a very useful Git command: git reset. See you in the next chapter!