Managing source code with git

by Zenon Harley

We use git as our revision control system of choice. This is a brief summary of some commonly-used commands.

Usage: Remote repository over SSH

We often access our source code repositories remotely over a secure shell (SSH) connection. In this example, user@server.com is the SSH login and src/my-project.git is the path of the git repository on the server.

First, the files are retrieved:

git clone user@server.com:src/my-project.git

This will create a directory called my-project in your current directory. Within my-project, you'll find all the source code, plus a special directory .git. This special directory contains the git data files, which includes the entire history of all the revisions to the project.

After you've made some changes to the source code, you can preview the changes with:

git status

If you're adding files to the source code, you do so like this:

git add <filepattern>

The filepattern can be files (separated by spaces), patterns (e.g. *.jpg), or simply a dot (.) to add all files.

All desired changes and additions must now be committed. It's a good idea to include a descriptive message of the changes as shown.

git commit -m 'Bug fixed in Main.java'

This commit stores the change in the local .git directory. Since the .git directory contains a full copy of the repository, the entire history can be viewed (including the recent change).

git log

To send the changes to the original remote repository (i.e. you are ahead of the master), it is necessary to push them.

git push --all

To update your local copy with changes made by others (i.e. you are behind the master), you pull changes.

git pull

More details and examples are provided in the git documentation.

Disclaimer

Use of any software or documentation on this page indicates your acceptance of our MIT-style license.