Menu

Git

This article describes a short introduction how to setup git and work with it.

First, download and install Git.

The first time you use Git, you have to setup some global config in ~/.gitconfig. The first time it is empty. Add configs with following commands:

1

2

git config--globaluser.name"yourName"

git config--globaluser.email your@mail.com

You can see all of your settings with this command:

1

git config--list

Now, create a new repository at github. Lets name the repository for this tutorial “test”. Then, we need to create a folder called test and init git in this repo. It is always good to create a README file. After that, we will do our first commit and link the folder to the remote repository. The commits are always localy in the created folder, only when the commited code are pushed, then it will be on the remote repo.

1

2

3

4

5

6

7

8

mkdir test

cd test

git init

touch README

git add README

git commit-m'first commit'

git remote add origin git@github.com:GITHUB_USERNAME/test.git

git push-uorigin master

The latests command above sets the branch as Upstream. So you can use after this command the following to push it to your repo.

1

git push

If you have an existing Git repo, you can push it to the remote repo as follows:

1

2

3

4

cd existing_git_repo

git remote add origin

git@github.com:GITHUB_USERNAME/REPO_NAME.git

git push-uorigin master

Now i will describe some small tips for a better use of Git.

– Create Tags.
Tags are used for versioning of your code. For example V0.1 and so on. Tags are allways created to the last commit. If you want to push the tags, do it 🙂

1

2

git tag tagname

git push--tags

– Create a new branch.
With the first command, you will create an new branch, the “-b” crates a new branch and set it as start point. The first push you have to do like the second command to push it and set it as Upstream.

1

2

git checkout-b

git push-uorigin

– Change to another branch.
If you want to set it as Upstream, you know how to do it 🙂

1

2

git checkout

git push-uorigin

– Add all files to the Git index or only a file.

1

2

git add-A

git add filename

– Remove files from the Git index.

1

git rm--cache filename

– Check the Git status to see in which branch you are and which files are new, witch are modified and so on.

1

git status

– Check which Git version is in use.

1

git--version

– Ignore some kind of files or folders when you add it to the Git index.
You need a file called .gitignore in the root directory of your created folder at the beginning.

1

touch.gitignore

In this file you can know write what you want exclude when you use the command

1

git add-A

Here is an example how you can ignore all dist and build folders. Write this in your .gitignore file:

1

2

build

dist

So, you can merge a branch back into for example to master branch: see also link

1

2

$git checkout master

$git merge branchName

– Clone a Subversion repository in a Git repository.
You dont need to create the folder for it. Go only i the folder in which you want to create the clone of the svn repo and type the following commands.

1

2

3

4

git svn clone-sSVN_REPO_URL LOCAL_DIR

cd LOCAL_DIR

git remote add origin git@github.com:GITHUB_USERNAME/REPO_NAME.git

git push origin master

In this case it would be useful to check in which branch you are after the clone if there are more.
Checkout the branch you like to use and create then a new branch and push it as a new branch to your repo. So you have a better difference from the original one.