Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
$ git config --global user.name "Lorem Ipsum"
$ git config --global user.email lorem@ips.um
$ git config --global color.ui true
gitk
, git-gui
Thank you, thank you. Let's get started. Max, play me over.
(gasps) Looks like someone forgot to feed Max!
git init
bramus in ~/ikdoeict/git-demo
$ git init
Initialized empty Git repository in /Users/bramus/Kaho/git-demo/.git/
bramus in ~/ikdoeict/git-demo
$
.git
folder containing
git status
to check
bramus in ~/ikdoeict/git-demo
$ touch test.txt
bramus in ~/ikdoeict/git-demo
$ git status
# …
#
# Untracked files:
# …
# test.txt
#
bramus in ~/ikdoeict/git-demo
$
git add
→ staginggit commit
→ committingbramus in ~/ikdoeict/git-demo
$ git add test.txt
bramus in ~/ikdoeict/git-demo
$ git commit -m 'Start tracking test.txt'
[master (root-commit) 9c43f96] Start tracking test.txt
0 files changed
create mode 100644 test.txt
bramus in ~/ikdoeict/git-demo
$
test.txt
is stored in the repository
9c43f96
bramus in ~/ikdoeict/git-demo on master
$ touch test2.txt
bramus in ~/ikdoeict/git-demo on master
$ echo 'hello' >> test.txt
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Changes not staged for commit:
# …
# modified: test.txt
#
# Untracked files:
# …
# test2.txt
#
bramus in ~/ikdoeict/git-demo on master*
$ git add test2.txt
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Changes to be committed:
# …
# new file: test2.txt
#
# Changes not staged for commit:
# …
# modified: test.txt
#
bramus in ~/ikdoeict/git-demo on master*
$ git commit -m 'Start tracking test2'
[master f1c6883] Start tracking test2
0 files changed
create mode 100644 test2.txt
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Changes not staged for commit:
# …
# modified: test.txt
#
bramus in ~/ikdoeict/git-demo on master*
$
$ git commit -am 'commitmessage'
$ git commit --amend -m 'commitmessage'
$ git add .
.txt
files
$ git add *.txt
$ git add subfolder/
$ git reset filename
bramus in ~/ikdoeict/git-demo on master
$ rm test2.txt
bramus in ~/ikdoeict/git-demo on master
$ git status
# On branch master
# Changes not staged for commit:
#
# modified: test.txt
# deleted: test2.txt
#
bramus in ~/ikdoeict/git-demo on master*
$ git add .
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Changes to be committed:
# …
# modified: test.txt
#
# Changes not staged for commit:
# …
# deleted: test2.txt
#
bramus in ~/ikdoeict/git-demo on master*
$ git add -u .
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Changes to be committed:
# …
# modified: test.txt
# deleted: test2.txt
#
#
bramus in ~/ikdoeict/git-demo on master*
$ git reset test.txt
Unstaged changes after reset:
M test.txt
bramus in ~/ikdoeict/git-demo on master*
$ git commit -m 'Deleted test2.txt'
[master 1d7a184] Deleted test2.txt
0 files changed
delete mode 100644 test2.txt
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Changes not staged for commit:
# …
# modified: test.txt
#
#
bramus in ~/ikdoeict/git-demo on master*
$ ls
drwxr-xr-x 4 bramus staff 136 Mar 19 21:18 .
drwxr-xr-x 10 bramus staff 340 Mar 19 15:49 ..
drwxr-xr-x 13 bramus staff 442 Mar 19 21:24 .git
-rw-r--r-- 1 bramus staff 6 Mar 19 20:44 test.txt
bramus in ~/ikdoeict/git-demo on master*
$ git commit -am 'Hello'
[master c53f9b6] Hello
1 file changed, 1 insertion(+)
bramus in ~/ikdoeict/git-demo on master
$
git add -u
git rm file
git diff file
to compare the current state with the previously committed state
bramus in ~/ikdoeict/git-demo on master
$ echo 'more hello' >> test.txt
bramus in ~/ikdoeict/git-demo on master*
$ git diff test.txt
diff --git a/test.txt b/test.txt
index ce01362..3477282 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
hello
+more hello
bramus in ~/ikdoeict/git-demo on master*
$
Recommended Sublime Text 2 Plugin: GitGutter
git log
to see the commit log
bramus in ~/ikdoeict/git-demo
$ git log
commit c53f9b6bd0e7271af7c5d856f84f51d9b749d77f
Author: Bramus <bramus@bram.us>
Date: Tue Mar 19 22:09:32 2013 +0100
Hello
commit 1d7a18481b5f6664d2cf30b1455c6ad1c041caa2
Author: Bramus <bramus@bram.us>
Date: Tue Mar 19 21:24:13 2013 +0100
Deleted test2.txt
commit f1c68836b8c1f91c16d4f4c0540fca0f251ff946
Author: Bramus <bramus@bram.us>
Date: Tue Mar 19 20:44:31 2013 +0100
Start tracking test2
commit 9c43f961b1defcc2f4f14526a2b20c4f3c267a62
Author: Bramus <bramus@bram.us>
Date: Tue Mar 19 16:02:03 2013 +0100
Start tracking test.txt
bramus in ~/ikdoeict/git-demo
$
q
to exit the commit loggit log --oneline
to get a concise output
bramus in ~/ikdoeict/git-demo
$ git log --oneline
c53f9b6 Hello
1d7a184 deleted test2.txt
f1c6883 Start tracking test2
9c43f96 Start tracking test.txt
bramus in ~/ikdoeict/git-demo
$
$ git log --author=name
$ git log --since="1 week ago"
git log
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
bramus in ~/ikdoeict/git-demo
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
* c53f9b6 - (HEAD, master) Hello (16 minutes ago) <Bramus>
* 1d7a184 - Deleted test2.txt (61 minutes ago) <Bramus>
* f1c6883 - Start tracking test2 (2 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (6 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo
$
git lg
for it
$ git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git lg
to get the output as shown above$ git init
$ git add file
$ git reset file
$ git commit -m 'message'
$ git diff file
$ git lg
Welcome to the Head Museum. I'm Leonard Nimoy.
bramus in ~/ikdoeict/git-demo on master
$ git lg
* c53f9b6 - (HEAD, master) Hello (15 hours ago) <Bramus>
* 1d7a184 - Deleted test2.txt (16 hours ago) <Bramus>
* f1c6883 - Start tracking test2 (17 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (22 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$ git checkout 1d7a184
Note: checking out '1d7a184'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
HEAD is now at 1d7a184... Deleted test2.txt
bramus in ~/ikdoeict/git-demo on (no branch)
$ git lg
* 1d7a184 - (HEAD) Deleted test2.txt (16 hours ago) <Bramus>
* f1c6883 - Start tracking test2 (17 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (22 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$ git checkout f1c6883
Previous HEAD position was 1d7a184... Deleted test2.txt
HEAD is now at f1c6883... Start tracking test2
bramus in ~/ikdoeict/git-demo on (no branch)
$ git lg
* f1c6883 - (HEAD) Start tracking test2 (17 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (22 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$ git checkout master
Previous HEAD position was f1c6883... Start tracking test2
Switched to branch 'master'
bramus in ~/ikdoeict/git-demo on master
$ git lg
* c53f9b6 - (HEAD, master) Hello (15 hours ago) <Bramus>
* 1d7a184 - Deleted test2.txt (16 hours ago) <Bramus>
* f1c6883 - Start tracking test2 (17 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (22 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$
$ git reset filename
$ git reset HEAD
$ git checkout -- filename
$ git reset HEAD --hard
$ git stash
Well, let's at least throw this TV away,
the batteries in the remote are gettin' low.
git clone url
bramus in ~/ikdoeict
$ git clone git@github.com:fabpot/Silex.git
Cloning into 'Silex'...
remote: Counting objects: 6612, done.
remote: Compressing objects: 100% (1908/1908), done.
remote: Total 6612 (delta 4225), reused 6317 (delta 4008)
Receiving objects: 100% (6612/6612), 1.28 MiB | 176 KiB/s, done.
Resolving deltas: 100% (4225/4225), done.
bramus in ~/ikdoeict
$ cd Silex
bramus in ~/ikdoeict/Silex on master
$ git log --oneline
59e7dbd merged branch davedevelopment/add-doc-info-to-docs (PR #647)
6a4fffe Added a note about writing documentation
8e6d30a merged branch davedevelopment/form-extension-points (PR #553)
7c38c9a Add services allowing for form type extensions and guessers
fd3ba62 merged branch GromNaN/remember-me (PR #645)
adc172b Add support for remember-me with RememberMeServiceProvider
fcd93b3 added a note about all possible options for the session (refs #633)
a31…
…
bramus in ~/ikdoeict/Silex on master
$
git clone url .
git remote add name url
bramus in ~/ikdoeict/git-demo on master
$ git remote add origin git@github.com:bramus/git-demo.git
bramus in ~/ikdoeict/git-demo on master
$ git push origin master
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 705 bytes, done.
Total 9 (delta 1), reused 0 (delta 0)
To git@github.com:bramus/git-demo.git
* [new branch] master -> master
bramus in ~/ikdoeict/git-demo on master
$
$ git push origin master
git push
and it'll default to origin
and master
$ git pull
origin/master
→ master
bramus in ~/ikdoeict/git-demo on master
$ git remote show
origin
bramus in ~/ikdoeict/git-demo on master
$ git remote show origin
* remote origin
Fetch URL: git@github.com:bramus/git-demo.git
Push URL: git@github.com:bramus/git-demo.git
HEAD branch: master
Remote branch:
master tracked
Local ref configured for 'git push':
master pushes to master (up to date)
Imagine that this line represents time. At some point in the past, the timeline skewed into this tangent creating an alternate 1985.
master
$ git branch
$ git branch experimental
$ git checkout experimental
head
will be moved to the new branchexperiment
branch based on the master
bramus in ~/ikdoeict/git-demo on master
$ git branch
* master
bramus in ~/ikdoeict/git-demo on master
$ git branch experiment
bramus in ~/ikdoeict/git-demo on master
$ git branch
experiment
* master
bramus in ~/ikdoeict/git-demo on master
$ git checkout experiment
Switched to branch 'experiment'
bramus in ~/ikdoeict/git-demo on experiment
$ git branch
* experiment
master
bramus in ~/ikdoeict/git-demo on master
$
bramus in ~/ikdoeict/git-demo on experiment
$ touch exp.txt
bramus in ~/ikdoeict/git-demo on experiment*
$ git add .
bramus in ~/ikdoeict/git-demo on experiment*
$ git commit -m 'Start tracking exp.txt'
[experiment a65b9d7] Start tracking exp.txt
0 files changed
create mode 100644 exp.txt
bramus in ~/ikdoeict/git-demo on experiment
$ git lg
* a65b9d7 - (HEAD, experiment) Start tracking exp.txt (7 seconds ago) <Bramus>
* c53f9b6 - (origin/master, master) Hello (15 hours ago) <Bramus>
* 1d7a184 - Deleted test2.txt (16 hours ago) <Bramus>
* f1c6883 - Start tracking test2 (17 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (22 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on experiment
$ git checkout master
Switched to branch 'master'
bramus in ~/ikdoeict/git-demo on master
$ ls
drwxr-xr-x 4 bramus staff 136 Mar 20 20:19 .
drwxr-xr-x 11 bramus staff 374 Mar 20 15:53 ..
drwxr-xr-x 14 bramus staff 476 Mar 20 20:19 .git
-rw-r--r-- 1 bramus staff 6 Mar 20 14:38 test.txt
bramus in ~/ikdoeict/git-demo on master
$ echo 'on the master right now' >> test.txt
bramus in ~/ikdoeict/git-demo on master*
$ git add .
bramus in ~/ikdoeict/git-demo on master*
$ git commit -m 'on the master'
[master 0ea8dd4] on the master
1 file changed, 1 insertion(+)
bramus in ~/ikdoeict/git-demo on master
$ git lg
* 0ea8dd4 - (HEAD, master) on the master (2 seconds ago) <Bramus>
* c53f9b6 - (origin/master) Hello (15 hours ago) <Bramus>
* 1d7a184 - Deleted test2.txt (16 hours ago) <Bramus>
* f1c6883 - Start tracking test2 (17 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (22 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$
git merge
otherbranch
are merged into the HEAD
bramus in ~/ikdoeict/git-demo on master
$ git merge experiment
Merge made by the 'recursive' strategy.
0 files changed
create mode 100644 exp.txt
bramus in ~/ikdoeict/git-demo on master
$ git lg
* 965d016 - (HEAD, master) Merge branch 'experiment' (5 seconds ago) <Bramus>
|\
| * a65b9d7 - (experiment) Start tracking exp.txt (6 minutes ago) <Bramus>
* | 0ea8dd4 - on the master (2 minutes ago) <Bramus>
|/
* c53f9b6 - (origin/master) Hello (22 hours ago) <Bramus>
* 1d7a184 - Deleted test2.txt (23 hours ago) <Bramus>
* f1c6883 - Start tracking test2 (24 hours ago) <Bramus>
* 9c43f96 - Start tracking test.txt (28 hours ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$
$ git branch -d branchname
-D
instead of -d
bramus in ~/ikdoeict/git-demo on master
$ git branch -d otherbranch
error: The branch 'otherbranch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D otherbranch'.
bramus in ~/ikdoeict/git-demo on master
$ git branch -D otherbranch
Deleted branch otherbranch (was e079840).
bramus in ~/ikdoeict/git-demo on master
$
bramus in ~/ikdoeict/git-demo on master
$ echo 'something for exp' >> exp.txt
bramus in ~/ikdoeict/git-demo on master*
$ git add .
bramus in ~/ikdoeict/git-demo on master*
$ git commit -m 'change exp.txt'
[master babbcd3] change exp.txt
1 file changed, 1 insertion(+)
bramus in ~/ikdoeict/git-demo on master
$ git checkout experiment
Switched to branch 'experiment'
bramus in ~/ikdoeict/git-demo on experiment
$ echo 'a different line for exp.txt' >> exp.txt
bramus in ~/ikdoeict/git-demo on experiment*
$ git add .
bramus in ~/ikdoeict/git-demo on experiment*
$ git commit -m 'change exp.txt (from experiment)'
[experiment 2925064] change exp.txt (from experiment)
1 file changed, 1 insertion(+)
bramus in ~/ikdoeict/git-demo on experiment
$ git checkout master
Switched to branch 'master'
bramus in ~/ikdoeict/git-demo on master
$ git merge experiment
Auto-merging exp.txt
CONFLICT (content): Merge conflict in exp.txt
Automatic merge failed; fix conflicts and then commit the result.
bramus in ~/ikdoeict/git-demo on master*
$ git status
# On branch master
# Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: exp.txt
#
bramus in ~/ikdoeict/git-demo on master*
$ cat exp.txt
<<<<<<< HEAD
something for exp
=======
a different line for exp.txt
>>>>>>> experiment
bramus in ~/ikdoeict/git-demo on master*
$ subl exp.txt
bramus in ~/ikdoeict/git-demo on master*
$ git add exp.txt
bramus in ~/ikdoeict/git-demo on master*
$ git commit
[master 80de07c] Merge branch 'experiment'
bramus in ~/ikdoeict/git-demo on master
$
<<<<<<< HEAD
something for exp
=======
a different line for exp.txt
>>>>>>> experiment
HEAD
$ git checkout --ours conflictedfile
otherbranch
$ git checkout --theirs conflictedfile
featurebranch
is completed, merge it into the master
bramus in ~/ikdoeict/git-demo on master
$ git lg
* 1e7f02e - (HEAD, master) A change comtd on master (12 seconds ago) <Bramus>
* 80de07c - First commit! (2 minutes ago) <Bramus>
bramus in ~/ikdoeict/git-demo on master
$ git checkout experiment
Switched to branch 'experiment'
bramus in ~/ikdoeict/git-demo on experiment
$ git lg
* 669ae62 - (HEAD, experiment) A 2nd change comtd on experiment (34 seconds ago) <Bramus>* a9bdacb - A change comtd on experiment(69 seconds ago) <Bramus>
* 80de07c - First commit! (2 minutes ago) <Bramus>
bramus in ~/ikdoeict/git-demo on experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: A change comtd on experiment
Applying: A 2nd change comtd on experiment
bramus in ~/ikdoeict/git-demo on experiment
$ git lg
* dba0673 - (HEAD, experiment) A 2nd change comtd on experiment (2 seconds ago) <Bramus>* 84484e4 - A change comtd on experiment(2 seconds ago) <Bramus>
* 1e7f02e - (master) A change comtd on master (54 seconds ago) <Bramus>
* 80de07c - First commit! (3 minutes ago) <Bramus>
bramus in ~/ikdoeict/git-demo on experiment
$
$ git add conflictedfile
$ git rebase --continue
$ git rebase --abort
$ git push origin localbranch:remotebranch
$ git push origin :remotebranch
$ git checkout -b localbranch remote/remotebranch
$ git remote update
Yes, we have to work together, and not
have this fight I was definitely winning.
# 1. Make sure the master is up-to-date
$ git checkout master
$ git pull
# 2. Create a featurebranch based on the master
$ git checkout -b featurebranch
# 3. Develop, stage, and commit your feature on the featurebranch.
When finished continue to step 4
# 4. Make sure the master is still up-to-date
$ git checkout master
$ git pull
# 5. Rebase the master onto the featurebranch
(skip this step if master remained unchanged since step 1)
$ git checkout featurebranch
$ git rebase master
# 6. Merge the featurebranch onto the master
$ git checkout master
$ git merge featurebranch
# 7. Push the master
$ git push origin master
# 8. Clean up
$ git branch -d featurebranch
# 9. Rinse. Repeat
master
, but on featurebranches
git pull --rebase
to minimize the situation and keep the master clean (#). From then on create a featurebranch and continuemaster
must at all times contain deployable code $ git revert HEAD~1
$ git rebase -i commithash
$ git rebase -i HEAD~2
PLEASE NOTE: GITHUB IS NOT GIT!
AGAIN: GITHUB IS NOT GIT!
If you don't like my code, Fork off!
featurebranch
featurebranch
when ready to your own github remote# 1. Create a fork on GitHub
e.g. clone fabpot/Silex to bramus/Silex
# 2. Clone your repo locally
$ git clone git@github.com:bramus/Silex.git
# 3. Create a featurebranch based on the master
$ git checkout -b featurebranch
# 4. Develop, stage, and commit your feature on the featurebranch.
When finished continue to step 5
# 5. Publish your featurebranch on Github
$ git push origin localbranch:remotebranch
# 6. Do a pull request via Github and await merging
# 7. Rinse. Repeat.
Make sure your master is in sync with the original master one!
(add the original repo as remote and `git fetch` and `git rebase original/master`)
# 1. Checkout your master branch
$ git checkout master
# 2. Add a new remote named 'otherdude'
$ git remote add otherdude git://github.com/otherdude/Project.git
# 3. Fetch information about all branches on the 'otherdude' remote
$ git fetch otherdude
# 4. Merge 'otherdude/featurebranch' into your master
$ git merge otherdude/featurebranch
# 5. Push your newly-merged branch back to GitHub
$ git push origin master
https://
, or git://
Let's go. If I say one more thing,
I might say it with my evening boot.
.gitignore
Thumbs.db
app.db
*.log
vendor/*
node_modules/*
config.php
.gitignore
must be committed into the repository
config.php
config.php
to .gitignore
and provide a config.sample.php
instead
git add -p file
bramus in ~/ikdoeict/ikdoeict-website on master*
$ git add -p index.html
diff --git a/index.html b/index.html
index 3fc8413..d7af84e 100644
--- a/index.html
+++ b/index.html
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<head>
- <title>Home | Ikdoeict.BE</title>
+ <title>Home | IkdoeICT.BE</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/normalize.css" />
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
@@ -176,7 +176,7 @@
</section>
<footer>
- <p>Copyright (c) 2012 IkdoeICT.be</p>
+ <p>Copyright (c) 2013 IkdoeICT.be</p>
</footer>
</div>
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? n
bramus in ~/ikdoeict/ikdoeict-website on master*
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: index.html
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: index.html
#
bramus in ~/ikdoeict/ikdoeict-website on master*
$ git commit -m 'title casing'
[master fc91e11] title casing
1 file changed, 1 insertion(+), 1 deletion(-)
bramus in ~/ikdoeict/ikdoeict-website on master*
$ git diff index.html
diff --git a/index.html b/index.html
index 7602651..d7af84e 100644
--- a/index.html
+++ b/index.html
@@ -176,7 +176,7 @@
</section>
<footer>
- <p>Copyright (c) 2012 IkdoeICT.be</p>
+ <p>Copyright (c) 2013 IkdoeICT.be</p>
</footer>
</div>
bramus in ~/ikdoeict/ikdoeict-website on master*
$ git add .
bramus in ~/ikdoeict/ikdoeict-website on master*
$ git commit -m 'copyright 2013 instead of 2012'
[master c36c7be] copyright 2013 instead of 2012
1 file changed, 1 insertion(+), 1 deletion(-)
bramus in ~/ikdoeict/ikdoeict-website on master
$
git-ftp.py
python git-ftp.py
cap deploy
git push heroku master
(Press ↓ to see some frequently asked ones)