Git - Getting Started


How can we create our own repository?

mkdir someDirectory
cd someDirectory
git init
git add
git add --all     # add those 'unknown' files
git rm --cached <file>... # undo, in case you forgot .gitignore, and you do not want to start over by removing the .git directory
git commit -m "message"
git remote add origin<git repo>.git
git push origin master

The above code initialize an empty git repository inside the root of your rails application and add all your files to the git repo. If you need to ignore files, you can edit the .gitignore file. We also provided a message to the commit, and added the remote origin, which will track our local development. Finally, we pushed the changes in our master branch to github.

How can we make our Git repository available to other developers via the Git protocol?

cd /my/repository/
touch .git/git-daemon-export-ok
git daemon

Notice the 'git daemon' line in the above code. Now other developers can fetch from your repository using git://your.machine/my/repository/.git/

How can we clone an existing Git repository?

git clone

By cloning the file, you have copied all versions of all files for a project. The command also automatically checks out — or retrieves for editing — a copy of the newest version of the project.

How can we clone a repository into a directory with another name?

To clone a repository into a directory with another name of your choosing, use the following command format:

git clone mydirectory

How can we set up a bare (= without working directory) repository (e.g. on a webserver)?

mkdir my-repo.git
cd my-repo.git
git --bare init
chmod a+x hooks/post-update # this is needed for HTTP transport
                                      # you need to populate this repository via push

I suppose the the above statements should be execute inside a directory that the web server is already serving. In other words, the my-repo.git directory must be created inside your web server document root.

How can we push changes to a remote repository?

After committing changes to our local repository, we may want to push those changes to a remote repository.

git push origin master

This command pushes changes from the local “master” branch to the remote repository named “origin”. For cloned repositories, Git will automatically give the name “origin” to the server from which you cloned and the name “master” to your local repository. However, these names can be changed by the user.

How can we read the git manual for each command from the command line?

git help command
git command --help
man git-command
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License