Getting Started with GIT and GITHUB

I have been involved in software development for over 20 years, and have used various version control software including SCCS, CVS, Perforce, Clearcase, Visual Sourcesafe, and SVN. Over time, SCM trend has changed from file locking mechanism to distributed version control and GIT is gaining popularity with growing users. As a software engineer, SCM is critical part of your daily chores but you don't want to spend much time maintaining it.

In a large software development project, you have a dedicated staff managing SCM and load building activities but you may not have such luxary in a smaller project. If you do not want to bombarded with such SCM tasks, GitHub may be a good alternative for storing your source repository using GIT.

Assuming that you have a complete source tree in your working directory, i.e. www folder, create a repository, add current working tree, and commit the change to the newly created repository.

% git config --global "John Doe"
% git config --global ""
% cd www
% git init
% git add .
% git commit -m "Initial commit"

Now, you have a local repository in your "www" folder. We'll push the change to the remote repository. You may wish to create a .gitignore file with a list of files and folders that you would like to exclude.

% git remote add origin{account}/{repository}.git
% git remote -v  // To view remote repositories.
% git push origin   // this will fail
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '{account}/{repository}.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.
% git push -f origin master
Counting objects: 4802, done.                                          
Compressing objects: 100% (4624/4624), done.                           
Writing objects: 100% (4802/4802), 274.62 MiB | 225 KiB/s, done.       
Total 4802 (delta 594), reused 0 (delta 0)                             
 + 6798f5e...5b6612e master -> master (forced update) 

Your remote repository now contains your entire source tree. Login to the target machine (i.e. your web server), and clone the repository.

% git init
% git remote add origin{account}/{repository}.git
% git pull -f origin master

If you've install GitHub for Windows, and launch Git Shell you'll have "origin" already setup. You'll have to modify the remote "origin" with the following command:

C:\Test Repository [master]> git remote set-url origin https://{username}{account}/{repository}.git


Q. If you're getting permission error when trying to add a file to the repository, you may have committed the file as a root.

error: insufficient permission for adding an object to repository database .git/objects

error: public/index.php: failed to insert into database
error: unable to index file public/index.php
fatal: updating files failed

# if you're getting an error above, change to {git repository} folder, i.e. ~/.git
# and run chown command.
# chown -R {User}.{Group} .

Q. If you're getting non-fast-forward error, you may want to try the following.

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '{account}/{repository}.git'

To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

% git push origin +master

Q. I have deliverately removed a file from my directory to restore from the repository. However, "git pull" won't restore the missing file(s).
A. Git pull keeps track of sequence of "git fetch" and "git merge". So if you delete the files from your local repository, doing a git pull will not restore them. Instead, use "git checkout" as shown below.

% git checkout -- index.php
% git checkout origin/master index.php

This command will update the requested file from the given branch (1st example from local repository, and 2nd one from the remote branch origin/master).

Q. Created a new repository, added a remote repository, and tried to pull files and it throws an error.
A. Git is complaining that it cannot validate the CA Certificate. Run the following command:

% git push origin
error: The requested URL returned error: 401 while accessing 

fatal: HTTP request failed

// The error above is from a incorrect password, so try it again with a correct password.

% git pull origin master
error:  while accessing https://{username}{Account}/{repository}.git/info/refs

fatal: HTTP request failed

% git config --global http.sslVerify false

Q. How do you discard local changes? The following error occurs:
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm '
as appropriate to mark resolution, or use 'git commit -a'.
A. You'll have to manually merge the conflict, or reset to origin/master. You may reset local changes (non-committed) by resetting your repository.

% git pull origin master
error: Your local changes to the following files would be overwritten by merge:
Please, commit your changes or stash them before you can merge.

% git reset --hard
% git reset --hard origin/master

Q. I'm trying to push or pull changes to the remote origin, but getting the "cannot open display" error.
A. You'll have to prevent the bash shell from attempting to launch the dialogue box.

% git push origin master
(gnome-ssh-askpass:13723): Gtk-WARNING **: cannot open display:


Q. If you already have your Git Remote "origin" defined, and would like to change URL.
A. Use "git remote set-url" command.

git remote set-url origin{account}/{repository}.git


Git Tutorial by Lars Vogel.