Git for non-programmers 1

Why use a version control system (VCS)?

Have you ever tried to add a new feature to, or change (refactor) an existing part of, a piece of software (or improve a section of a document) and eventually realised that you made it worse and you aren’t sure how to get back to where you started? Perhaps, to avoid such a situation, you now have cluttered directories filled with my-code-v1, my-code-v2-plus-that-other-feature etc., etc. and you’re not quite sure which one does what you want. Version control will help you to keep a log of your changes as well as snapshots of the code as you apply them; it will also allow you to collaborate far more easily but, for now, we will focus on a single editor and a linear history.

Beginnings

First make sure you have git installed either through a package manager or grab the appropriate package from the website. Once installed start your terminal emulator and see if

git help

gives you any output.

Initialise

Initialisation is the same whether you have code already written or you are starting from scratch you can either navigate to the directory you would like to work in

git init

or specify the path

git init /path/to/my/files

Example

I have a folder in my home directory where I keep code and a project I want to start called universe

git init /home/stripes/code/universe

Then I create a file called universe.py, a simple python script, in the project directory that looks like this:

print('Hello, Universe!')

Add or Stage

Now to add some files to your repository run one of the following commands

git add file-name
git stage other-file

‘stage’ is a synonym for add so keep an eye out if you are reading any other documentation. The same command is used to add changes so after your first commit is completed and you begin to make new changes you will add them using the same command.

Example

Following on from initialisation, to add universe.py to our git repository:

git add /home/stripes/code/universe/universe.py

Commit

Once you have all of the necessary files added you can make your first commit

git commit

Now, assuming you haven’t used git before, you should get the message ‘Please tell me who you are.’ You should follow the instructions there to add your name and email address to your commits and then run our last command again.

This time git should open up your default text editor with some text like this:


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       modified:   universe.py
#

The recommended style for a commit message is to start with a short, declarative statement of the change, e.g. Fix frobnicating problem or Add wibbly-wobbly feature followed by a blank line and optional elaboration. As this is our first commit it is traditional to use the message Initial commit so type it in, save the file and close your editor.

Initial commit

Add first bit of code that does something

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       modified:   universe.py
#

Conclusion

Well, now you should have a shiny new repository of code and are maybe wondering how this helps at all! Stay tuned for part two, please and thank you!

git 

See also