Many people think Git is too chaotic, or consider it a complex version control system. In fact, this is not the case. This article helps everyone quickly get started with Git.

Getting Started

Before using Git, you need to create a repository. You can use an existing directory as a Git repository or create an empty directory.

To use your current directory as a Git repository, we just need to initialize it.

git init

To use a directory we specify as a Git repository.

git init newrepo

From now on, we will assume you are in the root directory of the Git repository, unless otherwise stated.

Adding New Files

We have a repository, but it is empty. We can use the add command to add files.

git add filename

You can use add... to continue adding more files.

Committing Versions

Now that we have added these files, we want them to actually be saved in the Git repository.

To do this, we commit them to the repository.

git commit -m "Adding files"

If you do not use -m, an editor will appear for you to write your own comment message.

When we have modified many files and do not want to add each one individually, we can use the -a flag to have commit automatically submit local modifications.

git commit -a -m "Changed some files"

The -a option of the git commit command can make allmodified or deleted files that are already managed by Gitbe committed to the repository.

Note carefully: -a will not cause new files to be committed, only modified ones.

Publishing Versions

We first clone a repository from the server and upload it.

git clone ssh://example.com/~/www/project.git

Now after we make modifications, we can push to the server.

git push ssh://example.com/~/www/project.git

Fetching Updates

If you have already pushed as described above, the following command means the current branch automatically merges with the only tracking branch.

git pull

Update from a non-default location to the specified URL.

git pull http://git.example.com/project.git

Already past five minutes?

Deletion

If you want to delete files from the repository, we use rm.

git rm file

Branching and Merging

Branching is done locally and is fast. To create a new branch, we use the branch command.

git branch test

The branch command does not take us into the branch; it only creates a new branch. So we use the checkout command to switch branches.

git checkout test

The first branch, or main branch, is called "master".

git checkout master

Changes to other branches will not be reflected on the main branch. If you want to commit the changes to the main branch, you need to switch back to the master branch, and then use merge.

git checkout master
git merge test

If you want to delete a branch, we use the -d flag.

git branch -d test

Related Articles

Source: http://itmyhome.com/2015/01/git-five-minutes-tutorial