Github

Getting Started with Git

You may have heard of Git, and like me, were somewhat confused on how to get started with it. This article should give you the basic tools you need to get started. In this article, we’ll be working from a terminal/command prompt. If you’re on Linux, you should feel right at home here.

First, if you haven’t already, you’re going to need a working binary of Git installed on your system. Head over to Github and grab the version most appropriate for your OS.

To install git in Ubuntu from a terminal, use this command:

sudo apt-get install git

Note: You can also use Bitbucket instead if you want to create private repositories.

Public Keys

Once we have Git installed on our system, let’s make sure the machine we’re using has a public key. Type this into your terminal:

cat ~/.ssh/id_rsa.pub

If that file doesn’t exist, you can create it using ssh-keygen

Copy the contents of ~/.ssh/id_rsa.pub to your clipboard, then head to your account on Github.com, go to Account Settings, then select SSH Keys on the left hand side. Add your key here.

Now that we have that set up, let’s get some code on there. cd to the path where your code resides and run:

git init

You should see a message indicating that an empty repo has been created. This creates the .git/ path in that folder which serves many functions related to git.

Pointing your working tree to your Github Remote Repository

This is just a fancy way of saying “Let’s associate this folder with the repository on Github”, so Github knows where to put the files you push.
Let’s create an empty repository on Github that we can push our code to.
After you create the empty repo, use this command:

git remote add origin https://github.com/user/repo.git

Be sure to replace user with your github username and repo.git with your reponame.git. For example, I have a test repo called test, and my username is uabassguy, so I would use git remote add origin https://github.com/uabassguy/test.git.

Configure your Git Username

Note: You should only need to do this once per system.
Set your configuration as follows (replacing the Name and email with your own). This tells Git who you are when you push changes to repositories.

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

Replacing the name with your own and your email associated with your Github account.

Now we’re ready to add files! This is a 3 stage process, and as you get familiar with this, it will become easier to add files individually with comments associated with each change. For now let’s cover the basics: Add, Commit, Push

git add

To add all files and sub-directories in your working tree, use:

git add .

To add untracked files (newly created files that you want git to track), use:

git add -u

Or to do both in one command, use:

git add -A

git commit

Now we need to add a message to this commit. A commit consists of all the files you added using git add. Use:

git commit -m "description of my changes"

You can repeat the last two steps (git add and git commit) as many times as you want before proceeding to the next step.

git push

Finally, let’s push everything over to github so any other developers on the project can be aware of the files we have changed. Use this command:

git push origin master

This uses the refspec we defined earlier using git remote add origin
This command may fail if there’s new changes on the remote repository that we haven’t yet pulled down to our local working tree. For that we simply:

git pull origin master

to pull those changes down, then we can push our changes.

git status

By using git status we can figure out which files have changed. Additionally we can create a file in the base of our working tree called .gitignore to ignore files. An example of .gitignore might look something like this:

tmp/
error_log
wp-config.php

Based on the example above, git will completely ignore changes in the tmp/ folder, as well as the individual files error_log and wp-config.php in the base folder. Git is pretty smart when it comes to frameworks, and has several pre-configured .gitignore files for frameworks such as Magento, WordPress, and Drupal that will automatically ignore files that contain sensitive information such as database passwords and so on.

There’s much much much more to github than I can ever hope to cover in one article, so I hope this has at least given you an idea of where to start. Version Control is great for managing your files if anything ever gets lost, corrupted, or deleted. So even if you work alone, Git is a great thing to know about. Additionally, you can set up your remote files on a server and pull your changes from github, so it’s a great way to synchronize your files. Once you get a little better at it, you can explore ways to deploy code without having to ssh into the server and manually pull files from github.

Happy coding!

The following two tabs change content below.

Dave Wilson

Senior Engineer at BuyerQuest
Dave Wilson is a web developer focused on the newest technologies, Social Networking, SEO, and Marketing. I have been working with computers since the days of Apple ][ and MS-DOS.

Latest posts by Dave Wilson (see all)

Posted in Github.