Skip to content

Instantly share code, notes, and snippets.

@mepcotterell
Last active December 11, 2015 23:08
Show Gist options
  • Save mepcotterell/4674381 to your computer and use it in GitHub Desktop.
Save mepcotterell/4674381 to your computer and use it in GitHub Desktop.
Examples from class...

Git Examples

Configuration

The first thing you'll want to do is configure git on your nike account.

$ git config --global user.name "Your Name Here"
$ git config --global user.email "[email protected]"
$ git config --global --add color.ui true

By default, the git log looks pretty cluttered. We can setup a better looking version git lg by entering the following command:

$ git config --global alias.lg "log --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s [%an] %C(blue)[%ar]%Creset' --graph --abbrev-commit"

Also, you might want to add a git branches by entering the following command:

$ git config --global alias.branches "for-each-ref --sort=-committerdate refs/heads/ --format='%(objectname) %(objecttype) %(refname:short) [%(authorname)] [%(committerdate:short)]'"

Creating and Using a Local Repository

Now let's create a local repository called my-project. In order to do this, just create a directory with the same name, and issue the git init command from within the directory.

$ mkdir my-project
$ cd my-project
$ git init

You should see a message that indicates that an empty repository was initialized in that location. By default, you are now working with the master branch of the repository that was just initialized. We'll talk more about branches later.

Let's create some empty files to initialize our repository with:

$ touch README
$ touch Person.java

After creating those files, use the git status. You'll notice that the files we just created are marked as untracked. This means that they are not under version control. In order to add them to version control, you can use the git add command as follows:

$ git add README Person.java

Now, if you use the git status command, you'll see that git knows that two new files were created. However, these files are not in the repository yet. When you edit some files and add them for tracking, you're creating a pending changeset for the current branch. In order for those changes to get added to the repository, you need to commit the pending changeset. This can be done using the git commit command as follows:

$ git commit -m "added initial files"

If you use the git status command again, git will now tell you that there is nothing to commit on the current branch. However, if you use the git log (or git lg if you set it up earlier in the tutorial), you'll see that the changeset was added to the repository.

Branching Workflow

One commonly used workflow for using git is to branch off whenever you want to work on a baseline and merge back in after you've finished.

Creating a branch is easy. Let's create a branch called add-constructor and switch over to it.

$ git branch add-constructor
$ git checkout add-constructor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment