In my previous post I said I would do a Git tutorial. It’s been a little while, but here it is. This will be a very brief guide, intended to get you started straight away. So, onwards!
First of all you should set up your Git installation with your name and email:
git config --global user.name "Your name"
git config --global user.email "firstname.lastname@example.org"
In any directory, with or without files, simply type
git init to start a repository there. No other set up needed.
When you have some files in the directory type
git status. This will show you all the files that have changed and are untracked. (In any version control system you need to ‘track’ files you want to keep versions of.)
You may see some filename~ files. You most likely don’t want to track these, so type
nano .gitignore, and enter ‘*~’ (without quotes). Press Ctrl+O to save, and Ctrl+X to exit. The *~ wild card will match any files ending in ‘~’, and cause Git to ignore them.
git add filename.txt to start tracking your files. For example, you will want to add the .gitignore file from above, so type
git add .gitignore. You will generally want to track all your source code files but not compiled ones, such as .class for Java. In fact, you can add *.class to your .gitignore file to make sure they don’t get added.
Finally, you can type
git commit -m "Commit message here" to commit your new files.
The big difference
Unlike other version control systems, with Git you have to add files you want to commit to the ’staging area’ before committing. This means that each time you want to commit some changes, your work flow will be something like this:
$ git status# On branch master # Changed but not updated: # (use "git add ..." to update what will be committed) # # modified: test.txt # no changes added to commit (use "git add" and/or "git commit -a")
$ git add test.txt
$ git commit -m "Made a change"Created commit 2efe3ce: Made a change 1 files changed, 1 insertions(+), 0 deletions(-)
Alternatively, you can use
git commit -a
-m "Commit message here" to commit all of your tracked files.
That’s all you really need to know to get started! One final recommendation is to install Meld, a fantastic visual diff program, which will show you all the changes you’ve made since your last commit. To install type
sudo apt-get install meld. And then in your Git repository type
meld . (note the trailing dot meaning ‘use this folder’).
There is of course much more to Git, such as branches and pushing to remote servers, but I’ll finish this introduction here and, depending on the response and persuasion I get, possibly write a further guide in the future.