Skip to content

6. Common troubleshooting solutions

Scott Campit edited this page Feb 21, 2020 · 1 revision

While learning how to use version control software is an invaluable skill that will save you a ton of time, it is a steep learning curve riddled with challenges and troubleshooting.

Based on experiences from myself and others, I've compiled some of the most frequent issues beginners make when using version control, and how to solve them or how to figure out your particular solutions.

1. Removing files you didn't want to commit

Suppose you staged ALL the files in your local repo (not suggested), and ended up adding a file or files that you didn't want to commit (not ready for production, large file size, etc).

One way to remedy this is to use git rm <filename>. This will remove the specific file from the staging cache, which will allow you to push the code to your remote without pushing the undesired file.

However, what if we never want to track this file using Git and accidently upload this file onto GitHub? We can use echo <filename> >> .gitignore. The .gitignore file is self-explanatory: it makes Git untrack files that are listed in this file. The command adds the file name to the .gitignore file.

2. Undo commits

Suppose we make a commit, but realize that there are mistakes in the code, and want to fix these before pushing them out. We can fix our mistake using the following commands:

# Undo the last commit while keeping your changes
git reset --soft HEAD~1

# It's all junk, so undo the last commit and discard all the changes
git reset --hard HEAD~1

3. Editing a commit message

I've had those days where I drank a little too much coffee, and saved commits with several typos by accident. To fix your commits, you can type the following commands:

# To fix a short commit:
git commit --amend -m "New message"

# To fix a longer and more detailed commit:
git commit --amend

4. You have some merge conflicts

Ah, I've gotten this message several times when first learning how to use git. The following message pops up if there are changes in the same file at the same position from separate branches:

> Auto-merging <filename>
> CONFLICT (content): Merge conflict in <filename>
> Automatic merge failed; fix conflicts and then commit the result.

From here, we need to do something. The most straightforward way is to resolve the changes manually, but can be a pain in the ass if you have to do this for several files (commit frequently).

In the file(s) that have the conflicts, you should see the following pointers:

.
.
.
<<<<<<< HEAD
 <conflicted code from one branch>
=======
 <conflicted code from another branch>
>>>>>>> other branch
.
.
.

You can manually resolve this conflict from here.

And finally, if all else fails ...

# remove git tracking
rm .git

# initialize git again
git init

# Add the remote url
git remote add origin <url-to-remote>

# Get your remote branches
git fetch origin --all

# Try pushing your changes again
git add .
git commit -m "Fix issue"
git push origin <branchname>