Quick GIT SCM Howto
This is just a quick rundown that I put here so I can refer to when I forget various things ;-). I hope it is of use to someone.
Creating your local repository
Git is distributed remember so all the history is copied to every repository. I am currently working on a distributed file system, it is called distfs and I will be using that in my examples.
In your project directory I am sure you will have some
directory structure with all your working files in... often things like
src/
and bin/
.
git init
From the top project directory
mine
is /home/ben/distfs
Now... you want to exclude all those binary
files don't you? If not you are doing something wrong! For this we edit
a file in the .git
directory we just created.
cd .git nano info/exclude
It may be better to write a .gitignore
file and include the same information. A .gitignore
file
can be checked in and then will be taken with your project so others do
not check in compiled files... which always annoys me.
My project is a C project and I want to ignore object
files everywhere and all files in the bin/
directory so
this is what my file looks like:
# git-ls-files --others --exclude-from=.git/info/exclude # Lines that start with '#' are comments. # For a project mostly in C, the following would be a good set of # exclude patterns (uncomment them if you want to use them): *.[oa] *~ bin/*
Now you need to add your files to version control. You
can add them all individually but because we want to add them all and we
have written rules on which we would rather exclude it is good enough to
just add them with a directory definition. Once you have done that you
can check the files that will be under version control by running
git status
.
git add . git status
Output for me is (notice no bin/ prefix):
# On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: src/blockfunctions.c # new file: src/blockfunctions.h # new file: src/compile.sh # new file: src/configfunctions.c # new file: src/configfunctions.h # new file: src/directoryfunctions.c # new file: src/directoryfunctions.h # new file: src/fstest.c # new file: src/hello.c # new file: src/sha256.c # new file: src/sha256sum.c # new file: src/sha512.c # new file: testfs/.config #
Not happy? use git rm
and take out the
files you don't want. You will probably have to force it (e.g. git
rm -f testfs/.config
), then you can check again with git
status
.
Once you are happy, commit the current set of files.
git commit -m "Initial Commit"
Creating the Remote Bare Repository
mkdir distfs.git cd distfs.git git init --bare
The .git
is optional but seems to be convention
Now you have a bare git repository (that would probably be located on your server). Once done you should start the daemon, I usually start it from the place where all the other git repositories are but you don't have to.
git daemon --verbose --base-path=/home/git --enable=receive-pack
To be able to clone the repository with something
like: git clone git://192.168.10.3/distfs.git
you must
export the repository. To do this make a file called git-daemon-export-ok
in the
distfs.git directory or add --export-all to your git daemon
command. --enable=receive-pack
is to allow the git
push
command later, try without and see what happens.
touch git-daemon-export-ok
or
git daemon --verbose --export-all --base-path=/home/git
Done... you are now able to push a project to your server using the native git protocol.
git push git://192.168.10.3/distfs.git HEAD
Push the project from the client machine
And clone/pull on any other machine:
git clone git://192.168.10.3/distfs.git
Clone the project from the server machine
Using SSH
You already have an ssh daemon running, why not use this to clone your repository? It is easy, using the example above just do a git push/clone once you have a bare repository on your server.
git push ssh://192.168.10.3/home/git/distfs git clone ssh://192.168.10.3/home/git/distfs
Branching
Easy... to branch from current tree, first commit:
git commit -a -m "My Commit"
then:
git checkout -b mybranchname
or longhand:
git branch mybranchname git checkout mybranchname
or to branch from a specific point in history (a previous commit):
git checkout -b mybranchname 342b192
where 342b192
is the start of the hash
you want to branch from. Or longhand:
git branch mybranchname 342b192 git checkout mybranchname
To merge switch to master or whatever then:
git merge mybranchname
Don't care about a branch anymore? then delete it :-)
git branch -d mybranchname
Remember that branches are not pushed automatically to the server and so you must add the branch to your config or append the name to git push.
References: 1 2 3: Git Branch man page.
Helpful Links
More information to come.
git ls-files git help ignore cat .git/info/exclude nano .git/info/exclude nano .git/info/exclude git status git add nanoxml/src/ git status git add model/src/com/cheesmo/nzb/model/impl/NanoXMLNzbParser.java git add nzbclient/src/com/cheesmo/nzb/client/DefaultSSLSocketFactory.java git add nzbclient/src/com/cheesmo/nzb/client/DownloadSegment.java git add nzbclient/src/com/cheesmo/nzb/client/SegmentQueue.java git status nano .git/info/exclude git status git add nzbclient/README.txt git status man gitadd git help add git add -u git status git commit git push git diff