Basic Git Setup and Use

Basic Git Setup

I make extensive use of Git many times a day. I would say it is a very basic part of my work-flow, especially with note-taking, learning, and writing blog posts. I use it for keeping track of scripts that I am working on as part of my continuing educational process, so not only is it useful in terms of versioning, which to be honest I hardly if ever use, but also as a lightweight method of maintaining files across multiple machines. Cloud storage systems like Google Drive or Dropbox are too much in terms of bloat for such simple needs and not really easy to use on a headless Linux server accessed only via SSH. Also, being able to access previous versions is important when dealing with software.

These instructions assume a Debian/Ubuntu installation on both the local machine, or client, and the server – accessed via ssh – both with git installed. There is also Git for Windows that works well and from the bash shell utilizes the same commands.

It is important at this point to give some forethought to how you want to set up your Git folders. In the beginning I used one ‘big’ folder for all my repos and ended up running into trouble when one got corrupted. Then I kept every repo separate and ended up with a lot of separate folders that became difficult to keep track of. I’ve been trying to find that happy medium and I suggest you think about your directory structure and what projects you’ll be using git for. Sub-repositories are also possible but I find this to add an unnecessary level of complexity.

Setup Commands

On the server, accessed via ssh through the command line, we’ll create the directory, move into it, and initialize an empty git repository:

/server/$ mkdir gitfolder && cd gitfolder
/server/gitfolder/$ git init –bare

This folder is now set as your ‘primary’ repository, or origin, for all the git files that will stored and shared across your devices. Though all git repositories are essentially equal, it is important to note that you won’t really be able to access and make changes to the files here like in the ‘master’ directories on your client machines. Because of versioning and the way Git works, this folder will have a very different file structure than the ‘working’ folders.

Now we’ll do some work on our client machine where the files that we want to share and control versions are located. First we’ll move into this ‘master directory’, initialize it, perform some basic commands to configure git:

/master/folder$ git init
/master/folder$ git config –global “”
/master/folder$ git config –global “your name”

Next we prepare the files for sending to the server, and add the server location to git’s configuration (only needs to be done this first time):

/master/folder$ git add .
/master/folder$ git commit -m ‘first commit’ -a
/master/folder$ git remote add origin ssh://username@origin-server:/path/to/origin/folder
/master/folder$ git push origin master

In the above commands, change username, origin-server, port and path… to your specific variables. You’ll only need to add the origin’s location once, but the other commands will become quite familiar.

You’ll be prompted to enter your password and then be greeted with a list of the files that were pushed to the server.

Beautiful! You can now work locally and then push changes to your new git server.

In order to clone that fancy new directory onto another client, move to the directory that the git directory will be stored in – do not create a directory for the files as the entire directory will be cloned using this command.

git clone ssh://user@server:/path/to/folder

Again, this will create the directory with the same name and all the files it contains.

Making Changes

After everything is setup, my basic work-flow is to start by entering the working directory and checking for any changes using the commands fetch and diff, then merging them if so.

git fetch origin master
git diff origin/master ## press ‘q’ to quit the screen
git merge origin/master

Then, after working on the files, commit them and add them to the ‘central’ repository.

git add . ## don’t forget that period!
git commit -m ‘message’ -a
git push origin master

And there you have a very lightweight, easy to setup, version and file backup system that can be easily shared. Git is quite mature and there is (almost too much) documentation out there, though the basics can sometimes get lost in all the possible uses, which is what prompted me to record my own very simple use case.

Now git it!! (Sorry, couldn’t resist)

Leave a Reply