Part 1: Git or How I learned to stop worrying and love the troubleshooting

Git is a version control system (VCS) used to maintain almost every version of your document/source code without worry of losing it locally or remotely on the Series of Tubes, while allowing collaboration and merging of other people’s work on your same project. I say ‘almost’ since it’s dependent on you committing each change.

Git is quite popular because of the advantages of a Distributed VCS such as Mercurial or Bazaar, as opposed to the older Subversion or Perforce as Centralized VCS. Git was created by Linus Torvalds of Linux kernel fame and started up in 2005. It is the primary method, at least amongst all my independent and novice web developer friends and programmers, for version control of source code. If you want more information about its features, go search “wiki git” or the main hub http://www.github.com, which will have a ton more information. I wanted to make this blog post to myself and future other novice Git users as a troubleshooting guide.

Makersquare pushed Git since it was the primary means of connecting to their Learn App and to begin the prework I started months ago. Reading over Git’s advantages and usage, I began to realize how naive I used to save my work. I would create “My Book Report-v1.doc” or “Don’t delete this file-My Antonia_My Book-Report—v2-JT-edit.docx” files just to keep track of my files while maintaining versions of it. Then the dreaded “oops I deleted everything single thing and now i must start over” accidents *shudder*. Horrible times.

Git is both relief, harmony, and fragile, all in one. I’ve already saved my ass several times with Git’s system of commits and pushes, but I’ve also screwed up and accidentally removed everything. It’s a thing to wonder and carefully use at the same time.

In summary, here’s the natural, unperturbed flow path for no issues:

(Assuming you have a ‘git init’ on your directory of choice)

1. Modify/change files as necessary.
2. Add/stage the file for tracking
3. Commit the files to the repository
4. Push the data to the remote repository to share with others.

 

In the times that I’ve screwed up, here are the various troubleshooting tips I’ve kept whenever I got into trouble and couldn’t undo my changes easily or navigate to safe grounds. Hopefully these help others. I will add to the list as my life cycle of a web dev engineer ages:

 

1. Error with creating a non-existent repository

I fell onto this error whenever I attempted to push to an incorrectly named remote repo, with the ‘.git’ being the only difference between the two repositories:


✘ ~/code/mks/backend/pizza   master  git remote -v
origin https://github.com/lolptdr/pizza (fetch)
origin https://github.com/lolptdr/pizza (push)
~/code/mks/backend/pizza   master  git push origin master
Username for 'https://github.com': lolptdr
Password for 'https://lolptdr@github.com':
remote: Repository not found.
fatal: repository 'https://github.com/lolptdr/pizza/' not found
✘ ~/code/mks/backend/pizza   master  git remote remove origin
~/code/mks/backend/pizza   master  git remote add origin https://github.com/lolptdr/pizza.git
~/code/mks/backend/pizza   master  git remote -v
origin https://github.com/lolptdr/pizza.git (fetch)
origin https://github.com/lolptdr/pizza.git (push)
~/code/mks/backend/pizza   master  git push origin master

You can see that the two origins are different enough that my ‘push’ wouldn’t work. Check your repository names or copy+paste correctly!

2. Error when pushing, requiring a pulling

Whenever changes are pushed onto your repo from a location that is not your own, such as adding a ‘readme.md’ file to your repo online, you need to do a ‘git pull’ before you do a ‘git push’. Take the following error messages and hints with care. They are very helpful!


✘  ~/code/mks/backend/pizza   master  git push -u origin master
Username for 'https://github.com': lolptdr
Password for 'https://lolptdr@github.com':
To https://github.com/lolptdr/pizza.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/lolptdr/pizza.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

~/code/mks/backend/pizza   master  git pull origin master
From https://github.com/lolptdr/pizza
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 README.md
~/code/mks/backend/pizza   master  git push origin master
Username for 'https://github.com': lolptdr
Password for 'https://lolptdr@github.com':
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 1.53 KiB | 0 bytes/s, done.
Total 7 (delta 3), reused 0 (delta 0)
To https://github.com/lolptdr/pizza.git
53f35a9..d1b427d master -> master

3. Error with pushing to the wrong branch

When you decide to work off a forked repo or if you want to split your work up from the master branch, you must ensure you are working on the correct branch, otherwise Git won’t allow you to push your updates. Makes sense, no? Why would you push work to the wrong part of the repo?

In this instance, I was pushing to the wrong branch (master) and received a notice to pull after pushing to the wrong repo, thus receiving another wrong message to pull again. I realized that after I was on the right branch (library) that I need to push to that branch specifically.


~/code/mks/backend/library   library  git remote add origin https://github.com/lolptdr/library.git
✘  ~/code/mks/backend/library   library  git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/lolptdr/library
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
~/code/mks/backend/library   library  git push origin master (wrong branch to merge with!!)
Username for 'https://github.com': lolptdr
Password for 'https://lolptdr@github.com':
To https://github.com/lolptdr/library.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/lolptdr/library.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
~/code/mks/backend/library   library  git push origin library

To be continued…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s