In this tutorial, you will learn Git basics, and you will study how to do branching and merging, which are very important when you work as a team on a project.
$ git config --global user.name "Your Name" $ git config --global user.email "you@example.com"
Please use your actual email because it will be used as an identification of your Git commits for remote repository in the next tutorial. This information will be written to .gitconfig under your home directory.
$ git config user.name Your Name $ git config user.email you@example.com
Unlike SVN, you may use Git on your local project without any centralized server. To get started you will need to initialize a repository.
$ mkdir YOUR_PROJECT $ cd YOUR_PROJECT
$ git init Initialized empty Git repository in /YOUR_PROJECT/.git/
Now, this folder is already Git. From now on, every commit operation will put your changes into your local repository. You can copy this folder to another machine and it will work. You need to do your first commit to make this light up.
$ touch README.txt
$ git add README.txt $ git commit -m "First commit" [master (root-commit) 52b149c] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.txt
Now lets see what this visually looks like. (A) is the first commit. The default branch that gets created with Git is a branch names master. The pointer (*) means the reference to the commit that you are currently working on or what Git calls HEAD.
$ echo this is a README > README.txt
$ git add README.txt $ git commit -m "Second commit" [master f4a9975] Second commit 1 file changed, 1 insertion(+)
$ git log commit f4a997566f2289c40bc8895b9f64d25ebd6613f7 Author: *** Date: *** Second commit commit 52b149c7b167408831baed75b378ae909ed86349 Author: *** Date: *** First commit
Now lets see what this visually looks like. (B) is the second commit, master branch moves to commit (B) with the current pointer (*) along.
From log, 52b149c… represents the commit (A), while f4a9975… represents the commit (B). You may see the changes between commits by:
$ git diff 52b149c...f4a9975 diff --git a/README.txt b/README.txt index e69de29..79c4409 100644 --- a/README.txt +++ b/README.txt @@ -0,0 +1 @@ +this is a README
Besides, you may switch your working commit by checkout (Note that cat is the command to view contain of file):
$ git checkout 52b149c HEAD is now at 52b149c... First commit $ cat README.txt $ git checkout f4a9975 Previous HEAD position was 52b149c... First commit HEAD is now at f4a9975... Second commit $ cat README.txt this is a README
Make sure that you are at the commit (B) before following the next step.
$ git checkout -b bug123 Switched to a new branch 'bug123'
Notice that the new branch is created as a pointer to the current commit (B) and our current pointer (*) is moved to bug123 branch.
You may see your current branch by:
$ git branch * bug123 master
$ echo fix a bug >> README.txt
You may see the changes in README.txt by:
$ cat README.txt this is a README fix a bug
$ git add README.txt $ git commit -m "Fix bug123" [bug123 a1bf88f] Fix bug123 1 file changed, 1 insertion(+)
Now, this visually looks like:
You may see the changes between brunch master and bug123 by:
$ git diff master..bug123 diff --git a/README.txt b/README.txt index 79c4409..2c44363 100644 --- a/README.txt +++ b/README.txt @@ -1 +1,2 @@ this is a README +fix a bug
$ git checkout master Switched to branch 'master' $ git branch bug123 * master
Now, we switched back to master branch, the current pointer (*) is moved.
Let’s see what we have now in README.txt:
$ cat README.txt this is a README
$ git merge bug123 Updating f4a9975..a1bf88f Fast-forward README.txt | 1 + 1 file changed, 1 insertion(+) $ cat README.txt this is a README fix a bug