wiki:Basic Git Usage

This page contains a basic tutorial for working with git. There aren't any advanced concepts here, just the basics. In this tutorial we'll be working with a testing git repo and fake files.

Its always a good thing to remember that while working with Git is a decentralized VCS, even though you may choose to push changes all to one server, you don't have to. Each repo contains a full history and set of objects in the .git directory.

You will also only see a .git directory in the top level unlike svn where you have .svn in each sub directory as well as the top level.

Unlike svn, git uses hashes to identify commits instead of revision numbers.

These are just some basic tasks, the man pages can go into a lot more detail.

Initializing a Git Repo

Create a empty working directory:

mkdir git-tutorial
cd git-tutorial

Initialize the local Git Repo:

git init

Committing files to the Repo

Lets create a test file:

echo "Test Data" > testfile

Stage the file to be included into the next commit:

git add testfile

Finally commit the file to the repo:

git commit -m "Add a test file"

Viewing changes to files before committing them

First lets make a change to our test file:

echo "Different Test Data" > testfile

View the changes:

git diff

You could also execute the following or something similar:

git diff testfile

Listing files with changes

You can use the following command to list the status of all files not ignored by Git:

git status

You can also use the command on a path, the same as git diff.

Revert a file back to a specified commit

In this case we want to revert to the current working ref, so we wont specify one:

git checkout testfile

Reverting the entire working tree

You can use the following command to revert the entire working tree to the last commit in the branch you're working with:

git reset --hard

Creating a new branch from the current ref

Using git checkout you can start a new branch:

git checkout -b testing

Lets change the test file in this branch so we can identify it in the future:

echo "Testing Branch" > testfile
git add testfile
git commit -m "Change the test file for the Testing Branch"

Switching between branches

Using git checkout you can switch between branches. Switch back to master and check the testfile:

git checkout master
cat testfile

Switch back to testing and check the test file

git checkout testing
cat testfile

Listing Branches

The following will show you a list of branches:

git branch

Creating a tag from the current branch

Before we create a tag lets change the testfile:

echo "Testing Tag testing-1.0" > testfile
git add testfile
git commit -m "Change the test file before tagging."

Finally create a testing-1.0 tag:

git tag testing-1.0

Its good to know that when you create a Tag, you stay in the current branch. It doesn't switch its current working ref to the tag. Its also good to know that when you checkout a tag, you can't commit changes to the tag. If you want to make changes you should change the branch the tag was created from or create a new branch from the tag using git checkout -b.

Listing Tags

The following will show you a list of tags:

git tag
Last modified 16 years ago Last modified on Jun 14, 2009, 4:15:38 PM
Note: See TracWiki for help on using the wiki.