Quick lessons of git.
1. Creating a directory.
mkdir git-example
git init
Initialized empty Git repository in /home/mario/projakty/git-example/.git/
2. Git index files.
For new repository git index repository is empty. We can compare current status of file with git index status.
git status
Because before I created new file by vim readme.txt command I have such status:
# On branch master
#
# Initial commit
#
# Untracked files:
# (use “git add …” to include in what will be committed)
#
# readme.txt
nothing added to commit but untracked files present (use “git add” to track)
Quick view on git index.
git status -s
3. Adding file to index.
git add readme.txt
git status -s
A readme.txt
4. Changes (File was changed) I’m checking file index after changes
git status -s
And I get:
AM readme.txt
Now we should add this change, because git take to count git index content of file.
git add .
5. First commit.
When we update git status of our repository we can commit our changes i.e our index.
git commit -m "First commit"
Output
[master (root-commit) 405a6e8] First commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
and status is:git status -s
M readme.txt
6. We can get information about our comment by;
git log
commit 405a6e8f6418dbe3f094a9dfe0c4358b3f26c6d3
Author: Mariusz Saramak
Date: Thu Sep 15 01:06:12 2011 +0200First commit
The main different between git and svn is that revision of svn are a numbers. Revisions are counted from 1 to up.
In git each revision is sign by hash. Identifier of revision is hash.
7. Information about commit.
We know that to identify commit we need hash of commit. But because this is a hash in most cases first leathers will identify a commit.
Example:
git show 405a6e8f6418dbe3f094a9dfe0c4358b3f26c6d3
Will show this same what
git show 405a6e
Information about commit in git:
commit 405a6e8f6418dbe3f094a9dfe0c4358b3f26c6d3
Author: Mariusz Saramak
Date: Thu Sep 15 01:06:12 2011 +0200First commit
diff –git a/readme.txt b/readme.txt
new file mode 100644
index 0000000..0dec223
— /dev/null
+++ b/readme.txt
@@ -0,0 +1 @@
+Hello git
8. Use of git committing:
a) make changes
b) use git add .
c) use git commit -m "message"
8a) Git tags
We can give each commit a name: tag.
git tag 1.0 // we called current version, last commit "1.0" we give name for last commit.
git tag 0.9 405a6e8 //we call firs commit as a 0.9
git lg // log commits
result:
a236c71 – (HEAD, 1.0, master) Revert “Change 3″ (19 minutes ago Mariusz Saramak)
ff20614 – Change 3 (41 minutes ago Mariusz Saramak)
4972029 – Commit by git-gui (3 hours ago Mariusz Saramak)
5db4a75 – some modify (3 hours ago Mariusz Saramak)
405a6e8 – (0.9) First commit (3 weeks ago Mariusz Saramak)
9) GIT-GUI
Instalation (linux – ubuntu ):
sudo apt-get install git-gui
To run gui:
git gui
Then we use Ctrl+T to add file to commit, we write comment and we press commit button to accept.
10) Nice logs
For standard log print from git we use: git log
For quick and nicer logs we use:
git lg
Please see Git configuration – at the bottom of the page.
11) Revert changes:
11a) When we made changes, but we didn’t add change to index.
Example:
vim readme.txt
//make some changes
git status -s
result:
M readme.txt
Now we reverting change:
git checkout readme.txt
And that it: all changes from file are reverted.
11b) Revert changes from file which we add to index.
Example
vim readme.txt
//make some changes
git add readme.txt
git status -s
result:
M readme.txt
Reverting:
git reset readme.txt
git checkout readme.txt //this command is need to revert changes
11c) Revert committed code.
Example: we found issue in our last commit:
git lg
ff20614 – (HEAD, master) Change 3 (22 minutes ago Mariusz Saramak)
4972029 – Commit by git-gui (2 hours ago Mariusz Saramak)
5db4a75 – some modify (3 hours ago Mariusz Saramak)
405a6e8 – First commit (3 weeks ago Mariusz Saramak)
To revert ff20614 we use git revert:
git revert ff20614 //this command create another commit
after writing a comment and call
git lg
We will see:
a236c71 – (HEAD, master) Revert “Change 3″ (2 minutes ago Mariusz Saramak)
ff20614 – Change 3 (24 minutes ago Mariusz Saramak)
4972029 – Commit by git-gui (2 hours ago Mariusz Saramak)
5db4a75 – some modify (3 hours ago Mariusz Saramak)
405a6e8 – First commit (3 weeks ago Mariusz Saramak)
I.e we add next commit – which has our last reverted changes.
12) Diff
12a) Diff between prev -and last commit
When we want see differences between previous and current comment we use:
git show
result:
commit a236c71288ad0656c695c7a4e9ffe2458247de07
Author: Mariusz Saramak
Date: Sun Oct 2 23:26:12 2011 +0200Revert “Change 3″
This reverts commit ff20614057113e440d0ae44e72d478fafc674151.
diff –git a/readme.txt b/readme.txt
index dbfa415..7a58c35 100644
— a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,3 @@
Hello git
Changes
-Changes3
+Changes2
12b)diffs between any revisions
git diff 5db4a75..4972029
result:
diff –git a/readme.txt b/readme.txt
index 497df56..7a58c35 100644
— a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
Hello git
Changes
+Changes2
12b)diffs between any revisions by using tags
git diff 0.9..1.0
13) branches
13a) show current branch
By default, git creating one branch called master. When we don’t change this branch we works on master branch. We can check on which branch we currently works by:
git branch
result:
* master
As we see we have only one branch – master and this branch is active: please notice * before branch name.
13b)create new branch
When we want work for something new in the code and we know that it will be big – then we should use branches. To create new branch:
git branch nameofnewbranch //to create new branch.
git branch //to check branches
result:
* master
nameofnewbranch
Now we have 2 branches, and master branch is active (please see *)
Quick look on branches:
git branch -v
result:
* master a236c71 Revert “Change 3″
nameofnewbranch a236c71 Revert “Change 3″
13c) Switching between branches
git checkout nameofnewbranch
Switched to branch ‘nameofnewbranch’
git branch -v
master a236c71 Revert “Change 3″
* nameofnewbranch a236c71 Revert “Change 3″
Now nameofnewbranch is active.
13d) merging branches
Now please modify readme.txt in nameofnewbranch branch and commit changes by: git commit -a -m "new change in new branch" and switch to master branch:
git checkout master now modify readme.txt file in master branch.
and commit.
When we call git branch -v
* master b760cfc new change in master
nameofnewbranch 42d01dc new change in new branch
Now we have prepared environment to merge branches, because we made changes in this same file and in this same line we expect that we might have conflict.
git merge nameofnewbranch
git merge nameofnewbranch
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
Now we must resolve conflict, open readme.txt file and delete markers.
We should commit file after merge, git will generate commit message:
git log --pretty=raw
result (please notice that last commit has two parents ):
commit 81c58b2211f17bfafb14acc5b9f8fd0e8c580ad7
tree 9ebf975dd8d8f186d06b9ca9191f4121f3bdf123
parent b760cfc8732200ff4e26e08fd2739a55a4ade9b6 //as we see this is merge commit 2 parents
parent 42d01dc0dd607182e21918acc3e7e4966fc9bb7c // 2 parents
author Mariusz Saramak1317593851 +0200
committer Mariusz Saramak1317593851 +0200 Merge branch ‘nameofnewbranch’
Conflicts:
readme.txtcommit b760cfc8732200ff4e26e08fd2739a55a4ade9b6
tree 6b57bb754448f10507b9fed19017a4a30912e3c7
parent a236c71288ad0656c695c7a4e9ffe2458247de07
author Mariusz Saramak1317593453 +0200
committer Mariusz Saramak1317593453 +0200 new change in master
commit 42d01dc0dd607182e21918acc3e7e4966fc9bb7c
What git lg print ?
81c58b2 – (HEAD, master) Merge branch ‘nameofnewbranch’ (10 minutes ago Mariusz
b760cfc – new change in master (17 minutes ago Mariusz Saramak)
42d01dc – (nameofnewbranch) new change in new branch (25 minutes ago Mariusz Sar
a236c71 – (1.0) Revert “Change 3″ (62 minutes ago Mariusz Saramak)
ff20614 – Change 3 (84 minutes ago Mariusz Saramak)
4972029 – Commit by git-gui (3 hours ago Mariusz Saramak)
5db4a75 – some modify (4 hours ago Mariusz Saramak)
405a6e8 – (0.9) First commit (3 weeks ago Mariusz Saramak)
14) Remote repository
We can use remote repository:
14a) setup remote configuration:
git config --global user.name "Mariusz"
git config --global user.email mariopce@gmail.com
Next:
14b) add remote repository
git remote add origin git@github.com:mariopce/example.git
check if remote repository is added correctly
git remote -v
result:
origin git@github.com:mariopce/example.git (fetch)
origin git@github.com:mariopce/example.git (push)
initial push:
git push -u origin master
result:
Counting objects: 28, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (28/28), 2.29 KiB, done.
Total 28 (delta 4), reused 0 (delta 0)
To git@github.com:mariopce/example.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
This post has been created after watching http://ontwik.com/git-github/git-in-action/
Git configuration:
git config --global alias.lg "log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr %an)%Creset' --abbrev-commit --date=relative"
git config --global alias.wipe '!git reset --hard;git clean -fd'
git config --global color.ui auto
Please see https://gist.github.com/515937 too