Git: Getting started

The basic idea
Each time a change is made to your project, a snapshot can be made of the current state. This is called a "commit". Every commit is linked to the previous version, making it possible to traverse back in time.

This makes it possible to undo changes anytime. Git also allows to share those changes with developers working on other parts of the software.
Making it all work
Git is developped as a set of command-line tools. On top of these tools, other developers have built nice graphical frontends. Feel free to use whatever tool suites you best:
The command prompt
In this tutorial, the Git command prompt is used, but the concepts of the tutorial are easily applied to any graphical Git client. The command prompt is used for a few simple reasons:
- It has the latest features.
- It is universal across all platforms.
- It is mostly used in examples on the web.
- ...and it's not really that difficult to use!
Basic setup
First, You need to introduce yourself to git.
- Tips of Windows users
-
In the "Git bash" command prompt, you can auto-complete commands and parameters with the TAB key. Press TAB again to see a list of suggestions.
Some interactive Git commands open vim as default editor, which you can exit with :q! <enter>. By changing the core.editor option, the editor can be changed to Notepad2 for example.
- Enter these commands:
git config --global user.name "Diederik van der Boor" git config --global user.email "vdboor@...."
And optionally, configure some settings:
- Useful settings:
git config --global color.ui auto
# colorize all messages
git config --global alias.st status# alias "git status" as "git st"
git config --global core.editor kwrite# e.g. you dislike VIM as editor
You can also edit these settings in the ~/.gitconfig
file.
The basic commands
As quick reference, here are the basic commands to work with Git:
Importing the project
In the root directory of a project at your computer, type:
git init
Now, git is able to track files, and you can add them:
- Adding all files:
git add .
# adds the whole project folder
git commit -a# commits everything
Git opens your text editor where you can enter a commit message. Type the commit message, save the file, and quit.
To abort, just close the file without saving it.
- Tips for finding help
-
A basic overview of the Git commands can be found with:
git --helpEvery sub command has a
--help
option too, for example:
git config --helpYou can quit the manual page by pressing q.
Viewing changes
There are a few commands to get information about your history:
- Getting information:
git log
# show a log of changes
git status# see which files you changed
git diff# view uncommitted changes in the working copy
git show commitid# show one commit
Undoing local changes
There are a few commands to undo local changes:
- Undo local changes:
git checkout filename
# restore original, throw local changes away
git checkout -f# throw all local changes away (be careful!)
git reset --hard# the same
That's all you need!
You can use Git for your projects at your computer now. Each time a new feature is complete, you can commit it with git commit -a. New files need to be added first with git add.
In the next chapter, you'll find more tips and insights to fully get up speed.
blog comments powered by Disqus