Observorama

Git Notes

Creating a new repository

These steps are necessary each time you create a new repository.

First navigate to the root directory of the your app and initialize a new repository.

$ git init

The next step is to add the project files to the repository with the add command. Git will ignore the files specified in the .gitignore file in your application root directory.

$ git add .

You can see which files are in the staging area with status.

$ git status

To tell Git you want to keep the changes, use commit. The -m flag lets you add a message for the commit.

$ git commit -m "Initial commit"

You can see a list of your commit messages using the log command.

$ git log

Pushing a repository to Github

Create the repository in your Github account. Do not initialize the repository with a README.

Then:

$ git remote add origin git@github.com:<username>/first_app.git
$ git push -u origin master

Git branch, edit, commit, merge

Branch

Git is good at making branches, which are copies of a repository where you can make changes without modifying the parent files. In most cases, the parent repository is the master branch, and we can create a new topic branch by using checkout with the -b flag.

To create a branch called modify-README and switch to it.

git checkout -b modify-README

You can use git branch to list all the local branches (the asterisk * identifies which branch you’re currently on).

$ git branch

The master branch is insulated from changes you make to the topic branch, so even if you mess things up you can always abandon the changes by checking out the master branch and deleting the topic branch. We’ll see how to do this below.

Edit

After creating the topic branch, you can make your changes.

Commit

With the changes made, you can take a look at the status of your branch.

$ git status

At this point, you could use git add ., but Git provides the -a flag as a shortcut for the common case of committing all modifications to existing files.

$ git commit -a -m "Improve the README file"

Be careful about using the -a flag improperly. If you have added any new files to the project since the last commit, you still have to tell Git about them using git add first.

Note that the commit message is written in the present tense. Git models commits as a series of patches, and in this context it makes sense to describe what each commit does, rather than what it did. Moreover, this usage matches up with the commit messages generated by Git commands themselves.

Merge

Now that we’ve finished making our changes, we’re ready to merge the results back into our master branch.

$ git checkout master
$ git merge modify-README

After you’ve merged in the changes, you can tidy up your branches by deleting the topic branch using git branch -d if you’re done with it.

$ git branch -d modify-README

Abandoning changes to a branch

As mentioned above, it’s also possible to abandon your topic branch changes, in this case with git branch -D.

# For illustration only; don't do this unless you mess up a branch
$ git checkout -b topic-branch
$ <really screw up the branch>
$ git add .
$ git commit -a -m "Major screw up"
$ git checkout master
$ git branch -D topic-branch

Unlike the -d flag, the -D flag will delete the branch even though we haven’t merged in the changes.

Push to Github

Once you’ve made your changes you can push the changes up to GitHub to see the result. Since we have already done one push we can omit origin master and simply run git push.

$ git push

You should now see your changes in your Github repository.