Posts tagged ‘git’

Simple git example (remote repository)

In the last example we were working with a purely local repository. This example shows you how to work with remote repositories.

We’re going to convert the local repository we previously created in to a “bare” repository, this is a repository we can easily do “pushes” and “pulls” from (like commits and updates in svn). Once you’ve done this you won’t use this central repository to work on directly, it’ll just be where everything is stored.

0. On your “server”, convert the repository to be a “bare” repository.

cd testproject
git config --bool core.bare true

1. On your client “clone” (checkout in svn nomenclature) the repository.

This depends on how you’re accessing the repository but if it’s over ssh you can do this:

git clone ssh://[email protected]/home/myuser/testproject

2. Create a file and push it (commit in svn nomenclature):

touch remotetest
git add remotetest
git commit remotetest
git push

3. Pull the latest version (update in svn nomenclature):

git pull

Simple git example (local repository)

0. Configure your information:

git config --global user.name "Mr User the user"
git config --global user.email "[email protected]"

You can also configure an editor:

git config --global core.editor 'vim'

1. Create a repository

mkdir testproject
cd testproject
git init

2. Commit a file

touch testfile
git add testfile
git commit

3. Making a release branch

git branch RB_1.0 master

While you’ve created a release branch, you’re still currently on the master branch. So if you make a change here:

touch testfile2
git add testfile2
git commit

That file will NOT be in the release branch (RB_1.0).

4. Switching between the master branch and the release branch.

Unlike, SVN which stores different branches in different directories, git uses a command to switch your directory between branches. Slightly confusingly this command is called “checkout”.

git checkout RB_1.0

If you want, you can make changes to RB_1.0, they wont be refected elsewhere.

touch rb1file
git add rb1file
git commit

5. Releasing

Create a tag:

git tag 1.0 RB_1.0

List all tags:

git tag

6. Merging

Merge changes from RB_1.0 on to master:

git checkout master
git rebase RB_1.0

7. Delete a branch

It seems to be git practice to delete branches, you can always branch from the tag.

git branch -d RB_1.0

8. Creating a tarball

git archive --format=tar --prefix=testproject1 1.0 > ~/testproject1.tar

9. Changing a file!

This may be slightly different than you were expecting. You can’t just edit the file and type “git commit” rather, you need to add then commit. For example, after editing testfile:

git add testfile
git commit

Git creates commits in two stages like this. The first add “stages” the change and commit commits it. You can do multiple adds before the commit. The idea is to let you get everything organised before finally performing the commit.

Notes on git

There are no version numbers. A revision is identified by its hash.

master is like trunk in SVN/CVS.

checkout, doesn’t really mean checkout. It mean switch between branches.

You can also branch from tags the same way you branch from master.