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, the 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 a 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 luxury in a smaller project. If you do not want to be 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 the current working tree, and commit the change to the newly created repository.

% git config --global user.name "John Doe"
% git config --global user.email "[email protected]"
% 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 https://[email protected]/{account}/{repository}.git
% git remote -v  // To view remote repositories.
% git push origin   // this will fail
To https://[email protected]/{account}/{repository}.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/{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)                             
To https://[email protected]/{account}/{repository}.git             
 + 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 https://[email protected]/{account}/{repository}.git
% git pull -f origin master
If you've installed GitHub for Windows, and launched Git Shell you'll have "origin" already set up. You'll have to modify the remote "origin" with the following command:

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

FAQ

Q. If you're getting a 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 a non-fast-forward error, you may want to try the following.

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/{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 deliberately removed a file from my directory to restore it from the repository. However, "git pull" won't restore the missing file(s).

A. Git pull keeps track of the 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 the 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 threw 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 
        https://{username}@github.com/{Account}/{repository}/info/refs

fatal: HTTP request failed

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

% git pull origin master
error:  while accessing https://{username}@github.com/{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 will be overwritten by merge:
application/controller/Processor.php
Please, commit your changes or stash them before you can merge.

% git reset --hard
or
% 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:

% unset SSH_ASKPASS

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 https://[email protected]/{account}/{repository}.git

References

Git Tutorial by Lars Vogel.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment