Archive

Posts Tagged ‘git’

Getting Started With Git and GitHub on Mac

January 5th, 2014 No comments

I recently got a new Mac and had to go through the process of connecting to GitHub. Here is some notes that I took during this process.

We start our journey at GitHub.com. If you are new to GitHub, you would need to create an account before proceeding. Once you are logged in to GitHub, hit the “Create a New Repo” button on the top right.

1

Enter your new repo information as shown below. I have asked GitHub to create a README file and added MIT License. If you are not sure what license to use, GitHub provides a website to help you with your decision. GitHub also allows you to add a .gitignore file with predefined ignore patterns while creating a repo.

2

Once GitHub creates a repo, you will be redirected to the repo page and you can see the contents of the repo.
3

This concludes our setup on GitHub. The next step is to checkout the repo on to your Mac. Before we do that, check and make sure you have Git instlled on your Mac:
4

Since I have already installed XCode for iOS App development, Git gets installed during that process. If you don’t have Git installed, you can download it from GitHub site.

The next step is to configure Git. Open a Terminal and run the following commands:

 $git config --global user.name "Your Name"
 $git config --global user.email "Your Email Address"
 $git config --global apply.whitespace nowarn

Since we are running on Mac, let’s make sure that .DS_Store folders are ignored globally. To do this, run the following command:

  git config --global.core.excludesfile ~/.global_gitignore
  echo .DS_Store >> ~/.global_gitignore

In the past, I have used SSH Keys to connect Mac to GitHub. Moving forward, I will be using Https instead. One drawback with Https is that it would require us to enter our username and password everytime we talk to GitHub. To fix this, we will use osxkeychain credential helper to cache our username and password. Before you move forward run the following command to ensure that osxkeychain is installed.

git credential-osxkeychain
usage: git credential-osxkeychain 

Now, configure Git to use osxkeychain credential helper:

   $git config --global credentail.helper osxkeychain

We are finally ready to checkout the GitHub repo. Run the following command:

  git clone https://github.com/YOUR_USERNAME/YOUR_REPO_NAME 

If you have everything setup correctly, Git would create a folder with your repo name and checkout files. You should see a result similar to this:
5

Go to the repo and run git status. You should get message “nothing to commit, working directory clean”.

Let’s modify READEME.md file using your favourate editor. Now running git status shows that we have modified READEME.md.
6

Let’s commit README file by running the following command:

  git commit -am "Updated README"

The final step in this process is to push our changes over to GitHub by running the following command:

  git push origin master

If you are running this for the first time, you will be prompted to enter username and password. Git will cache these and will not prompt you in future. Since, I have this information cached, here is what I get:
7

Go to GitHub.com repo and see that your changes are on the repo:
8

Categories: Git, Solutions Log Tags: , ,

Getting Started With Git and GitHub

April 22nd, 2012 No comments

For the last two years, I have been using Git on a windows machine. Recently, I had to install Git on my Mac and connect to Github. Since I have not blogged for such a long time, I thought of jotting down my notes here.

Setting Up Git Locally

1. The first step in this process is to download and install Git locally. The easiest way to do is to download the Git OS X installer from http://code.google.com/p/git-osx-installer/downloads/list The recent version at the time of writing is 1.7.9.4.

Once the download is complete, just run the installer. Upon successful installation, run the following command:

2. The next step is to configure Git. To do this, run the following commands from the command line:

git config –global user.name “YOUR_NAME”
git config –global user.email “YOUR_EMAIL”

3. Now we are ready to create a Git repository. To do that, create a new directory where you want to store your project files. For this blog, I have created a new folder called “hellogit” and moved to the folder in the terminal.

To create a new repository, simply run the following command in the project folder:

4. Now we are ready to add files to our repository. Assume that we have two files in our project test.html and test2.html. Move the files into the hellogit project folder.

Now run the git status command to know the latest status of our repository:

Git comes back saying that we have two untracked files. Git does not automatically track the files in our repository. Instead we need to explicitly add them.

5. To add files to Git, simply run the following command:

If you are new to Git, you might be wondering about the lack of feedback from Git about the add command. Just remember that upon a successful command execution, Git typically does not give you any feedback.

Let’s run the status command to see what happened:

From the above image, you can see that Git added our two files. It also mentions that we have a OS specific file DS_Store that needs to be tracked. We will address that in a minute.

6. When we are done making changes to our project files, we can check them in. To do that, simply run the following command:

In the above command, I have given a commit message “Initial Commit”.

7. Often we will end up with project and OS specific files (DS_Store) in our project folder that we don’t want to check in to Git. We can tell Git to ignore certain files and never track them. To do that, we simply create a .gitignore file that has the name(s) of the file that needs to be ignored.

Now, when the .gitignore file is in place, run the status command and you will see that the .DS_Store file is no longer tracked.

Preparing For Github

Before you can start using Github remote repository, you need to setup SSH keys. These keys are needed to establish secure connection between your computer and Github. To do that follow these steps:
1. Run the following command:

The No such file or directory indicates that you don’t have any SSH keys. Sometimes, the folder might exist but it might not have the id_rsa* SSH key files.

2. The next step is to create a new SSH key. Run the following command:

Hit enter to accept the default location. You will be asked to enter a passphrase. Enter a pass phrase and make a note of it as we will need it again.

Connecting To Github

1. Before we can push our code to a github repository, we need to create an account. Go to http://github.com and create a new account.

2. Once you have created and logged into your account, you need to give github your SSH key. In order to do that, go to your account settings page and click the SSH Keys Navigation Item:

3. Copy the SSH key you created above. To do that, simply go to the .ssh folder and type the following command:

4. Click the Add SSH Key button and enter the key as well as a title.

5. Go back to the terminal and try to connect to github using this command:

When you accept Yes, you will be asked to enter the passphrase:

Check the “Remember Password in keychain” and hit Ok.

6. Now, we are ready to start pushing code to Github. Before we do that we need to create a remote repository on Github. To do that, click the New Repository button:

Enter the following information for the new repository:

Make sure you leave all the fields in their default state.

7. Once the repository has been created on Github, move to your terminal and cd.. into the hellogit project. Then run the following two commands (replace bava with your username):

Now, go back to your Github repository https://github.com/YOUR_USER_NAME/hellogit and you will see that the files have been pushed.

Categories: Git, Solutions Log Tags: ,