KEMBAR78
Git Introduction Tutorial | PDF
/tutorial/git
Thomas Rausch
thomas.rausch@tuwien.ac.at
Institute for Information Systems
Distributed Systems Group
TU Wien
http://git-scm.com
Of Linus and learning curves
“Linus is a guy who delights in being cruel to people …”
Of Linus and learning curves
“Linus is a guy who delights in being cruel to people …”
“His latest cruel act is to create a revision control system
which is expressly designed to make you feel less
intelligent than you thought you were” [1]
[1]: Tech Talk: Linus Torvalds on git - http://youtu.be/4XpnKHJAok8
Of Linus and learning curves
What you should take from today
What you should take from today
git =
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
$ git config --global user.name "Thomas Rausch"
$ git config --global user.email thomas@rauschig.org
Configure your user
basics stages branch merge remotes advanced
$ git init
Initialized empty Git repository in /home/thomas/git-tutorial/.git/
Initialize an empty repository
$ git clone <repo> [<directory>]
Clone a remote repository
basics stages branch merge remotes advanced
Check the status of your repository
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# README.md
# src/
nothing added to commit but untracked files present (use "git add" to track)
$ git status -sb
# Initial commit on master
?? README.md
?? src
basics stages branch merge remotes advanced
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README.md
#
$ git add README.md
Start tracking files
basics stages branch merge remotes advanced
Commit changes
$ git status
# On branch master
nothing to commit (working directory clean)
$ git commit -m "add readme file"
[master (root-commit) d4c59ff] add readme file
1 file changed, 3 insertions(+)
create mode 100644 README.md
basics stages branch merge remotes advanced
View differences of current unstaged modifications
$ git diff --color
diff --git a/src/Main.java b/src/Main.java
index 66c8e93..dcdf6cb 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,5 +1,7 @@
class Main {
+ static int status = 0;
+
public static void main(String[] args) {
- System.exit(0);
+ System.exit(status);
}
}
basics stages branch merge remotes advanced
Unstaging files
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: src/HelloWien.java
# new file: src/HelloWorld.java
$ git reset HEAD src/HelloWien.java
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: src/HelloWorld.java
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# src/HelloWien.java
#
basics stages branch merge remotes advanced
Undoing local unstaged changes
$ git status
# On branch master
# 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: README.md
$ git checkout -- README.md
$ git status
# On branch master
nothing to commit (working directory clean)
basics stages branch merge remotes advanced
Viewing the history
$ git log
commit 9c3cb834c43d67cc37b15e74b64dc830c1e78199
Author: Thomas Rausch <thomas@rauschig.org>
Date: Mon Oct 14 00:04:53 2013 +0100
modified readme file
commit d4c59ffd7e676dad6aef2cc244b87e3c579aa904
Author: Thomas Rausch <thomas@rauschig.org>
Date: Sun Oct 13 23:45:39 2013 +0100
add readme file
$ git log --graph --pretty=format:'%Cred%h%Creset %an: %s - %Creset %C(yellow)%d
%Creset %Cgreen(%cr)%Creset' --abbrev-commit -date=relative
* 9c3cb83 Thomas Rausch: modified readme file - (HEAD, master) (4 minutes ago)
* d4c59ff Thomas Rausch: add readme file - (24 minutes ago)
$ git log --oneline
9c3cb83 modified readme file
d4c59ff add readme file
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
working directory
basics stages branch merge remotes advanced
working directory
staging area
basics stages branch merge remotes advanced
working directory
staging area
repository
basics stages branch merge remotes advanced
working directory
staging area
repository
git add
basics stages branch merge remotes advanced
working directory
staging area
repository
git add
git commit
basics stages branch merge remotes advanced
working directory
staging area
repository
git commit -a
basics stages branch merge remotes advanced
working directory
staging area
repository
basics stages branch merge remotes advanced
git reset HEAD <file>
working directory
staging area
repository
git reset HEAD <file> git checkout
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
$ git branch
develop
* master
new-feature
List branches
basics stages branch merge remotes advanced
$ git branch
develop
* master
new-feature
List branches
Branch you have
currently
checked out
basics stages branch merge remotes advanced
$ git checkout develop
Switched to branch 'develop'
Change into a branch
basics stages branch merge remotes advanced
Manage branches
$ git branch <branch>
Create a new branch from the one you have currently checked out
$ git branch -m <oldbranch> <newbranch>
Rename a branch
$ git branch -D <branch>
Delete a branch
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
Merge branches
$ git checkout master
$ git merge topic
Merge the specified branch into the current branch (the one you have checked out)
Merges topic
into master
basics stages branch merge remotes advanced
master
topic
master
topic
basics stages branch merge remotes advanced
master
topic
master
topic
Fast-forward
topic
basics stages branch merge remotes advanced
master
master, topic
Fast-forward
basics stages branch merge remotes advanced
master
topic
master
topic
basics stages branch merge remotes advanced
master
topic
master
topic
Recursive three-way
basics stages branch merge remotes advanced
master
topic
A merge commit
with 2 parents
Recursive three-way
basics stages branch merge remotes advanced
Conflicts
basics stages branch merge remotes advanced
master
basics stages branch merge remotes advanced
class Main {
public static void main(String... args) {
}
}
master
basics stages branch merge remotes advanced
class Main {
public static void main(String... args) {
}
}
master topic
basics stages branch merge remotes advanced
class Main {
public static void main(String... args) {
}
}
class Main {
static int status = 0;
public static void main(String... args) {
System.exit(status);
}
}
master topic
basics stages branch merge remotes advanced
class Main {
public static void main(String... args) {
System.exit(0);
}
}
class Main {
public static void main(String... args) {
}
}
class Main {
static int status = 0;
public static void main(String... args) {
System.exit(status);
}
}
master topic
basics stages branch merge remotes advanced
class Main {
public static void main(String... args) {
System.exit(0);
}
}
class Main {
public static void main(String... args) {
}
}
class Main {
static int status = 0;
public static void main(String... args) {
System.exit(status);
}
}
class Main {
static int status = 0;
public static void main(String... args) {
<<<<<<< HEAD
System.exit(0);
=======
System.exit(status);
>>>>>>> topic
}
}
master topic
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
master
develop
topic (features)
basics stages branch remotes advanced
master
merge
basics stages branch remotes advanced
git branch develop
master
develop
merge
basics stages branch remotes advanced
master
develop
merge
basics stages branch remotes advanced
master
develop
merge
basics stages branch remotes advanced
master
develop
feature
git branch featureX
merge
basics stages branch remotes advanced
master
develop
feature
feature
git branch featureY
merge
basics stages branch remotes advanced
master
develop
feature
feature
git branch featureY
merge
basics stages branch remotes advanced
master
develop
feature
feature
git merge featureX
merge
basics stages branch merge remotes advanced
master
develop
feature
feature
git merge develop
basics stages branch merge remotes advanced
master
develop
feature
feature
version 1.0
basics stages branch merge remotes advanced
master
develop
feature
feature
git merge develop
downmerge
basics stages branch merge remotes advanced
master
develop
feature
feature
version 2.0
basics stages branch merge remotes advanced
master
develop
feature
feature
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
working directory
staging area
repository
basics stages branch merge remotes advanced
working directory
staging area
local repository
remote repository
basics stages branch merge remotes advanced
working directory
staging area
local repository
remote repository
working directory
staging area
local repository
working directory
staging area
local repository
developer developer developer
basics stages branch merge remotes advanced
working directory
staging area
local repository
remote repository
working directory
staging area
local repository
working directory
staging area
local repository
developer developer developer
origin
basics stages branch merge remotes advanced
working directory staging area local repository remote repository
basics stages branch merge remotes advanced
working directory staging area local repository remote repository
git add/mv/rm
git commit
git commit -a
git reset <file>
git checkout <branch>
basics stages branch merge remotes advanced
working directory staging area local repository remote repository
git push
git add/mv/rm
git commit
git commit -a
git reset <file>
git checkout <branch>
basics stages branch merge remotes advanced
working directory staging area local repository remote repository
git push
git fetch
git add/mv/rm
git commit
git commit -a
git reset <file>
git checkout <branch>
basics stages branch merge remotes advanced
working directory staging area local repository remote repository
git push
git fetch
git pull
git add/mv/rm
git commit
git commit -a
git reset <file>
git checkout <branch>
basics stages branch merge remotes advanced
Subversion
working
directory
basics stages branch merge remotes advanced
Subversion
working
directory
remote
repository
basics stages branch merge remotes advanced
commit
Subversion
working
directory
remote
repository
basics stages branch merge remotes advanced
updatecommit
Subversion
working
directory
remote
repository
basics stages branch merge remotes advanced
updatecommit
working
directory
Subversion Git
working
directory
remote
repository
basics stages branch merge remotes advanced
updatecommit
local
repository
working
directory
Subversion Git
working
directory
remote
repository
basics stages branch merge remotes advanced
updatecommit
local
repository
working
directory
remote
repository
Subversion Git
working
directory
remote
repository
basics stages branch merge remotes advanced
Distribution models
basics stages branch merge remotes advanced
Distribution models
Centralized workflow
origin
basics stages branch merge remotes advanced
Distribution models
Centralized workflow
origin
GitHub Forking/Pull Request
dictator
basics stages branch merge remotes advanced
Distribution models
Centralized workflow
origin
GitHub Forking/Pull Request
upstream
dictator developers
fork
origin
basics stages branch merge remotes advanced
Distribution models
Centralized workflow
origin
GitHub Forking/Pull Request
upstream
dictator developers
fork
origin
basics stages branch merge remotes advanced
Distribution models
Centralized workflow
origin
GitHub Forking/Pull Request
Pull request
upstream
dictator developers
origin
fork
basics stages branch merge remotes advanced
Distribution models
Centralized workflow
origin
GitHub Forking/Pull Request
Pull request
upstream
dictator developers
origin
fork
Many others
basics stages branch merge remotes advanced
Managing remotes
$ git remote -v
origin git@github.com:thrau/openengsb-framework.git (fetch)
origin git@github.com:thrau/openengsb-framework.git (push)
upstream git@github.com:openengsb/openengsb-framework.git (fetch)
upstream git@github.com:openengsb/openengsb-framework.git (push)
$ git remote add <name> <url>
$ git remote rm <name>
basics stages branch merge remotes advanced
Remote tracking branches
$ git branch -a
basics stages branch merge remotes advanced
Remote tracking branches
$ git branch -a
* master
my-local-feature
remotes/origin/master
basics stages branch merge remotes advanced
Remote tracking branches
$ git branch -a
* master
my-local-feature
remotes/origin/master
originlocal
master
master,
origin/master
basics stages branch merge remotes advanced
Remote tracking branches
$ git branch -a
* master
my-local-feature
remotes/origin/master
originlocal
master
master,
origin/master
basics stages branch merge remotes advanced
Remote tracking branches
$ git branch -a
* master
my-local-feature
remotes/origin/master
master
origin/master
originlocal
master
basics stages branch merge remotes advanced
Remote tracking branches
$ git push
master,
origin/master
originlocal
master
basics stages branch merge remotes advanced
Delete remote branches
$ git push origin :branchname
basics stages branch merge remotes advanced
Delete local tracking branches
$ git fetch origin --prune
basics stages branch merge remotes advanced
Dealing with remote conflicts
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
master,
origin/master
originlocal
master
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
To ssh://thomas@localhost/home/thomas/git-remote
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
To ssh://thomas@localhost/home/thomas/git-remote
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull origin master
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
To ssh://thomas@localhost/home/thomas/git-remote
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull origin master
… fix merge conflicts ...
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
To ssh://thomas@localhost/home/thomas/git-remote
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull origin master
… fix merge conflicts ...
$ git commit -am "Merge remote branch 'master'"
$ git push origin master
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
To ssh://thomas@localhost/home/thomas/git-remote
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull origin master
… fix merge conflicts ...
$ git commit -am "Merge remote branch 'develop'"
$ git push origin master
Produces a
merge commit
basics stages branch merge remotes advanced
Dealing with remote conflicts
$ git push origin master
To ssh://thomas@localhost/home/thomas/git-remote
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git pull --rebase origin master
… fix merge conflicts ...
$ git rebase --continue
repeat
$ git push origin master
Rewrites the
history.
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
Rewriting history
basics stages branch merge remotes advanced
Rewriting history
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic
master
topic
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic
“base”
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic
“base”
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic
“base”
* * **
* new commits
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Rewriting history
master
topic * * **
Avoid changing
published histories!
$ git rebase master
Rebase the current branch on to the tip of a different one
basics stages branch merge remotes advanced
Ups, I forgot something
basics stages branch merge remotes advanced
Ups, I forgot something
$ git commit --amend -a
Add all your current changes to the previous commit
basics stages branch merge remotes advanced
Ups, I forgot something
$ git commit --amend -a
$ git commit --amend -m "new commit message"
Reword the last commit
Add all your current changes to the previous commit
basics stages branch merge remotes advanced
Ups, I forgot something
$ git commit --amend -a
Add all your current changes to the previous commit
$ git commit --amend -m "new commit message"
Reword the last commit
Avoid changing
published histories!
basics stages branch merge remotes advanced
Pushing a rewritten history
$ git push --force origin <branch>
Danger zone! Overwrites the remote history with your local one. Remote commits may get lost!
basics stages branch merge remotes advanced
When that merge came in like a wrecking ball
$ git merge --abort
$ git rebase --abort
Abort an initiated merge or rebase
basics stages branch merge remotes advanced
Git tag
$ git tag
Show all tags
$ git tag -a v2.0
Tag the current commit with an annotation
Tag (mark) important points in the commit history, e.g. when a working version is released
$ git push origin --tags
Push tags to the remote
basics stages branch merge remotes advanced
Ignoring files
Create a file in your repository (and add & commit it) named .gitignore, containing paths and
rules that tell git which files to ignore.
# Maven files
target/
bin/
# Eclipse project files
.project
.classpath
.settings
# Mac OS
.DS_Store
# IntelliJ IDEA files
*.iml
*.ipr
*.iws
.idea
# backup-files
*~
basics stages branch merge remotes advanced
Writing good commit messages
basics stages branch merge remotes advanced
Writing *bad* commit messages
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
“changes”
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
“changes”
“it works!”
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
“changes”
“it works!”
“final commit”
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
“changes”
“it works!”
“final commit”
“Testing in progress ;-)”
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
“changes”
“it works!”
“final commit”
“Testing in progress ;-)”
“TODO: write meaningful commit message”
basics stages branch merge remotes advanced
Writing *bad* commit messages
”fix”
“:(:(“
“changes”
“it works!”
“final commit”
“Testing in progress ;-)”
“TODO: write meaningful commit message”
“Your commit is writing checks your merge can't cash”
basics stages branch merge remotes advanced
Writing good commit messages
Write commit messages as if you're giving commands to the codebase
basics stages branch merge remotes advanced
Writing good commit messages
Write commit messages as if you're giving commands to the codebase
“Add DAO interfaces for Entities“
basics stages branch merge remotes advanced
Writing good commit messages
Write commit messages as if you're giving commands to the codebase
“Add DAO interfaces for Entities“
“Implement basic version of AddressDAO”
basics stages branch merge remotes advanced
Writing good commit messages
Write commit messages as if you're giving commands to the codebase
“Add DAO interfaces for Entities“
“Implement basic version of AddressDAO”
“Fix bug in delete method of UserDAO”
“Move package security to at.ac.tuwien.service”
“Add generated fxml files for UI”
basics stages branch merge remotes advanced
basics stages branch merge remotes advanced
Other great tutorials
●
Official Git Documentation
http://git-scm.com/doc
●
TryGit – An interactive Git tutorial
http://try.github.io
●
Git Immersion
http://gitimmersion.com/
●
Atlassian Git Tutorials
https://www.atlassian.com/git
●
Git Cheatsheet – Command categorisation
http://ndpsoftware.com/git-cheatsheet.html
●
LearnGitBranching
http://pcottle.github.io/learnGitBranching
Further reading
●
A successful Git branching model
http://nvie.com/posts/a-successful-git-branching-model/
●
Changing history, or How to Git pretty
http://justinhileman.info/article/changing-history
●
Reset Demystified
http://git-scm.com/blog/2011/07/11/reset.html
●
Avoiding Git Disasters: A Gory Story
http://randyfay.com/node/89
●
A Rebase Workflow for Git
http://randyfay.com/node/91
Headache?
Questions?
fin
Thanks for listening - Enjoy git!
Feedback appreciated
thomas.rausch@tuwien.ac.at

Git Introduction Tutorial

  • 1.
    /tutorial/git Thomas Rausch thomas.rausch@tuwien.ac.at Institute forInformation Systems Distributed Systems Group TU Wien
  • 2.
  • 3.
    Of Linus andlearning curves “Linus is a guy who delights in being cruel to people …”
  • 4.
    Of Linus andlearning curves “Linus is a guy who delights in being cruel to people …” “His latest cruel act is to create a revision control system which is expressly designed to make you feel less intelligent than you thought you were” [1] [1]: Tech Talk: Linus Torvalds on git - http://youtu.be/4XpnKHJAok8
  • 5.
    Of Linus andlearning curves
  • 6.
    What you shouldtake from today
  • 7.
    What you shouldtake from today git =
  • 8.
    basics stages branchmerge remotes advanced
  • 9.
    basics stages branchmerge remotes advanced
  • 10.
    basics stages branchmerge remotes advanced $ git config --global user.name "Thomas Rausch" $ git config --global user.email thomas@rauschig.org Configure your user
  • 11.
    basics stages branchmerge remotes advanced $ git init Initialized empty Git repository in /home/thomas/git-tutorial/.git/ Initialize an empty repository $ git clone <repo> [<directory>] Clone a remote repository
  • 12.
    basics stages branchmerge remotes advanced Check the status of your repository $ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README.md # src/ nothing added to commit but untracked files present (use "git add" to track) $ git status -sb # Initial commit on master ?? README.md ?? src
  • 13.
    basics stages branchmerge remotes advanced $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README.md # $ git add README.md Start tracking files
  • 14.
    basics stages branchmerge remotes advanced Commit changes $ git status # On branch master nothing to commit (working directory clean) $ git commit -m "add readme file" [master (root-commit) d4c59ff] add readme file 1 file changed, 3 insertions(+) create mode 100644 README.md
  • 15.
    basics stages branchmerge remotes advanced View differences of current unstaged modifications $ git diff --color diff --git a/src/Main.java b/src/Main.java index 66c8e93..dcdf6cb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,7 @@ class Main { + static int status = 0; + public static void main(String[] args) { - System.exit(0); + System.exit(status); } }
  • 16.
    basics stages branchmerge remotes advanced Unstaging files $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: src/HelloWien.java # new file: src/HelloWorld.java $ git reset HEAD src/HelloWien.java $ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: src/HelloWorld.java # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # src/HelloWien.java #
  • 17.
    basics stages branchmerge remotes advanced Undoing local unstaged changes $ git status # On branch master # 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: README.md $ git checkout -- README.md $ git status # On branch master nothing to commit (working directory clean)
  • 18.
    basics stages branchmerge remotes advanced Viewing the history $ git log commit 9c3cb834c43d67cc37b15e74b64dc830c1e78199 Author: Thomas Rausch <thomas@rauschig.org> Date: Mon Oct 14 00:04:53 2013 +0100 modified readme file commit d4c59ffd7e676dad6aef2cc244b87e3c579aa904 Author: Thomas Rausch <thomas@rauschig.org> Date: Sun Oct 13 23:45:39 2013 +0100 add readme file $ git log --graph --pretty=format:'%Cred%h%Creset %an: %s - %Creset %C(yellow)%d %Creset %Cgreen(%cr)%Creset' --abbrev-commit -date=relative * 9c3cb83 Thomas Rausch: modified readme file - (HEAD, master) (4 minutes ago) * d4c59ff Thomas Rausch: add readme file - (24 minutes ago) $ git log --oneline 9c3cb83 modified readme file d4c59ff add readme file
  • 19.
    basics stages branchmerge remotes advanced
  • 20.
    basics stages branchmerge remotes advanced
  • 21.
    working directory basics stagesbranch merge remotes advanced
  • 22.
    working directory staging area basicsstages branch merge remotes advanced
  • 23.
    working directory staging area repository basicsstages branch merge remotes advanced
  • 24.
    working directory staging area repository gitadd basics stages branch merge remotes advanced
  • 25.
    working directory staging area repository gitadd git commit basics stages branch merge remotes advanced
  • 26.
    working directory staging area repository gitcommit -a basics stages branch merge remotes advanced
  • 27.
    working directory staging area repository basicsstages branch merge remotes advanced git reset HEAD <file>
  • 28.
    working directory staging area repository gitreset HEAD <file> git checkout basics stages branch merge remotes advanced
  • 29.
    basics stages branchmerge remotes advanced
  • 30.
    basics stages branchmerge remotes advanced
  • 31.
    basics stages branchmerge remotes advanced
  • 32.
    basics stages branchmerge remotes advanced $ git branch develop * master new-feature List branches
  • 33.
    basics stages branchmerge remotes advanced $ git branch develop * master new-feature List branches Branch you have currently checked out
  • 34.
    basics stages branchmerge remotes advanced $ git checkout develop Switched to branch 'develop' Change into a branch
  • 35.
    basics stages branchmerge remotes advanced Manage branches $ git branch <branch> Create a new branch from the one you have currently checked out $ git branch -m <oldbranch> <newbranch> Rename a branch $ git branch -D <branch> Delete a branch
  • 36.
    basics stages branchmerge remotes advanced
  • 37.
    basics stages branchmerge remotes advanced
  • 38.
    basics stages branchmerge remotes advanced
  • 39.
    basics stages branchmerge remotes advanced Merge branches $ git checkout master $ git merge topic Merge the specified branch into the current branch (the one you have checked out) Merges topic into master
  • 40.
    basics stages branchmerge remotes advanced master topic master topic
  • 41.
    basics stages branchmerge remotes advanced master topic master topic Fast-forward
  • 42.
    topic basics stages branchmerge remotes advanced master master, topic Fast-forward
  • 43.
    basics stages branchmerge remotes advanced master topic master topic
  • 44.
    basics stages branchmerge remotes advanced master topic master topic Recursive three-way
  • 45.
    basics stages branchmerge remotes advanced master topic A merge commit with 2 parents Recursive three-way
  • 46.
    basics stages branchmerge remotes advanced Conflicts
  • 47.
    basics stages branchmerge remotes advanced master
  • 48.
    basics stages branchmerge remotes advanced class Main { public static void main(String... args) { } } master
  • 49.
    basics stages branchmerge remotes advanced class Main { public static void main(String... args) { } } master topic
  • 50.
    basics stages branchmerge remotes advanced class Main { public static void main(String... args) { } } class Main { static int status = 0; public static void main(String... args) { System.exit(status); } } master topic
  • 51.
    basics stages branchmerge remotes advanced class Main { public static void main(String... args) { System.exit(0); } } class Main { public static void main(String... args) { } } class Main { static int status = 0; public static void main(String... args) { System.exit(status); } } master topic
  • 52.
    basics stages branchmerge remotes advanced class Main { public static void main(String... args) { System.exit(0); } } class Main { public static void main(String... args) { } } class Main { static int status = 0; public static void main(String... args) { System.exit(status); } } class Main { static int status = 0; public static void main(String... args) { <<<<<<< HEAD System.exit(0); ======= System.exit(status); >>>>>>> topic } } master topic
  • 53.
    basics stages branchmerge remotes advanced
  • 54.
    basics stages branchmerge remotes advanced master develop topic (features)
  • 55.
    basics stages branchremotes advanced master merge
  • 56.
    basics stages branchremotes advanced git branch develop master develop merge
  • 57.
    basics stages branchremotes advanced master develop merge
  • 58.
    basics stages branchremotes advanced master develop merge
  • 59.
    basics stages branchremotes advanced master develop feature git branch featureX merge
  • 60.
    basics stages branchremotes advanced master develop feature feature git branch featureY merge
  • 61.
    basics stages branchremotes advanced master develop feature feature git branch featureY merge
  • 62.
    basics stages branchremotes advanced master develop feature feature git merge featureX merge
  • 63.
    basics stages branchmerge remotes advanced master develop feature feature git merge develop
  • 64.
    basics stages branchmerge remotes advanced master develop feature feature version 1.0
  • 65.
    basics stages branchmerge remotes advanced master develop feature feature git merge develop downmerge
  • 66.
    basics stages branchmerge remotes advanced master develop feature feature version 2.0
  • 67.
    basics stages branchmerge remotes advanced master develop feature feature
  • 68.
    basics stages branchmerge remotes advanced
  • 69.
    basics stages branchmerge remotes advanced working directory staging area repository
  • 70.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository
  • 71.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository working directory staging area local repository working directory staging area local repository developer developer developer
  • 72.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository working directory staging area local repository working directory staging area local repository developer developer developer origin
  • 73.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository
  • 74.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository git add/mv/rm git commit git commit -a git reset <file> git checkout <branch>
  • 75.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository git push git add/mv/rm git commit git commit -a git reset <file> git checkout <branch>
  • 76.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository git push git fetch git add/mv/rm git commit git commit -a git reset <file> git checkout <branch>
  • 77.
    basics stages branchmerge remotes advanced working directory staging area local repository remote repository git push git fetch git pull git add/mv/rm git commit git commit -a git reset <file> git checkout <branch>
  • 78.
    basics stages branchmerge remotes advanced Subversion working directory
  • 79.
    basics stages branchmerge remotes advanced Subversion working directory remote repository
  • 80.
    basics stages branchmerge remotes advanced commit Subversion working directory remote repository
  • 81.
    basics stages branchmerge remotes advanced updatecommit Subversion working directory remote repository
  • 82.
    basics stages branchmerge remotes advanced updatecommit working directory Subversion Git working directory remote repository
  • 83.
    basics stages branchmerge remotes advanced updatecommit local repository working directory Subversion Git working directory remote repository
  • 84.
    basics stages branchmerge remotes advanced updatecommit local repository working directory remote repository Subversion Git working directory remote repository
  • 85.
    basics stages branchmerge remotes advanced Distribution models
  • 86.
    basics stages branchmerge remotes advanced Distribution models Centralized workflow origin
  • 87.
    basics stages branchmerge remotes advanced Distribution models Centralized workflow origin GitHub Forking/Pull Request dictator
  • 88.
    basics stages branchmerge remotes advanced Distribution models Centralized workflow origin GitHub Forking/Pull Request upstream dictator developers fork origin
  • 89.
    basics stages branchmerge remotes advanced Distribution models Centralized workflow origin GitHub Forking/Pull Request upstream dictator developers fork origin
  • 90.
    basics stages branchmerge remotes advanced Distribution models Centralized workflow origin GitHub Forking/Pull Request Pull request upstream dictator developers origin fork
  • 91.
    basics stages branchmerge remotes advanced Distribution models Centralized workflow origin GitHub Forking/Pull Request Pull request upstream dictator developers origin fork Many others
  • 92.
    basics stages branchmerge remotes advanced Managing remotes $ git remote -v origin git@github.com:thrau/openengsb-framework.git (fetch) origin git@github.com:thrau/openengsb-framework.git (push) upstream git@github.com:openengsb/openengsb-framework.git (fetch) upstream git@github.com:openengsb/openengsb-framework.git (push) $ git remote add <name> <url> $ git remote rm <name>
  • 93.
    basics stages branchmerge remotes advanced Remote tracking branches $ git branch -a
  • 94.
    basics stages branchmerge remotes advanced Remote tracking branches $ git branch -a * master my-local-feature remotes/origin/master
  • 95.
    basics stages branchmerge remotes advanced Remote tracking branches $ git branch -a * master my-local-feature remotes/origin/master originlocal master master, origin/master
  • 96.
    basics stages branchmerge remotes advanced Remote tracking branches $ git branch -a * master my-local-feature remotes/origin/master originlocal master master, origin/master
  • 97.
    basics stages branchmerge remotes advanced Remote tracking branches $ git branch -a * master my-local-feature remotes/origin/master master origin/master originlocal master
  • 98.
    basics stages branchmerge remotes advanced Remote tracking branches $ git push master, origin/master originlocal master
  • 99.
    basics stages branchmerge remotes advanced Delete remote branches $ git push origin :branchname
  • 100.
    basics stages branchmerge remotes advanced Delete local tracking branches $ git fetch origin --prune
  • 101.
    basics stages branchmerge remotes advanced Dealing with remote conflicts
  • 102.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master
  • 103.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master master, origin/master originlocal master
  • 104.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master To ssh://thomas@localhost/home/thomas/git-remote ! [rejected] HEAD -> master (non-fast-forward) error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  • 105.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master To ssh://thomas@localhost/home/thomas/git-remote ! [rejected] HEAD -> master (non-fast-forward) error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ git pull origin master
  • 106.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master To ssh://thomas@localhost/home/thomas/git-remote ! [rejected] HEAD -> master (non-fast-forward) error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ git pull origin master … fix merge conflicts ...
  • 107.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master To ssh://thomas@localhost/home/thomas/git-remote ! [rejected] HEAD -> master (non-fast-forward) error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ git pull origin master … fix merge conflicts ... $ git commit -am "Merge remote branch 'master'" $ git push origin master
  • 108.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master To ssh://thomas@localhost/home/thomas/git-remote ! [rejected] HEAD -> master (non-fast-forward) error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ git pull origin master … fix merge conflicts ... $ git commit -am "Merge remote branch 'develop'" $ git push origin master Produces a merge commit
  • 109.
    basics stages branchmerge remotes advanced Dealing with remote conflicts $ git push origin master To ssh://thomas@localhost/home/thomas/git-remote ! [rejected] HEAD -> master (non-fast-forward) error: failed to push some refs to 'ssh://thomas@localhost/home/thomas/git-remote' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. $ git pull --rebase origin master … fix merge conflicts ... $ git rebase --continue repeat $ git push origin master Rewrites the history.
  • 110.
    basics stages branchmerge remotes advanced
  • 111.
    basics stages branchmerge remotes advanced
  • 112.
    basics stages branchmerge remotes advanced Rewriting history
  • 113.
    basics stages branchmerge remotes advanced Rewriting history $ git rebase master Rebase the current branch on to the tip of a different one
  • 114.
    basics stages branchmerge remotes advanced Rewriting history master topic master topic $ git rebase master Rebase the current branch on to the tip of a different one
  • 115.
    basics stages branchmerge remotes advanced Rewriting history master topic “base” $ git rebase master Rebase the current branch on to the tip of a different one
  • 116.
    basics stages branchmerge remotes advanced Rewriting history master topic “base” $ git rebase master Rebase the current branch on to the tip of a different one
  • 117.
    basics stages branchmerge remotes advanced Rewriting history master topic $ git rebase master Rebase the current branch on to the tip of a different one
  • 118.
    basics stages branchmerge remotes advanced Rewriting history master topic $ git rebase master Rebase the current branch on to the tip of a different one
  • 119.
    basics stages branchmerge remotes advanced Rewriting history master topic “base” * * ** * new commits $ git rebase master Rebase the current branch on to the tip of a different one
  • 120.
    basics stages branchmerge remotes advanced Rewriting history master topic * * ** Avoid changing published histories! $ git rebase master Rebase the current branch on to the tip of a different one
  • 121.
    basics stages branchmerge remotes advanced Ups, I forgot something
  • 122.
    basics stages branchmerge remotes advanced Ups, I forgot something $ git commit --amend -a Add all your current changes to the previous commit
  • 123.
    basics stages branchmerge remotes advanced Ups, I forgot something $ git commit --amend -a $ git commit --amend -m "new commit message" Reword the last commit Add all your current changes to the previous commit
  • 124.
    basics stages branchmerge remotes advanced Ups, I forgot something $ git commit --amend -a Add all your current changes to the previous commit $ git commit --amend -m "new commit message" Reword the last commit Avoid changing published histories!
  • 125.
    basics stages branchmerge remotes advanced Pushing a rewritten history $ git push --force origin <branch> Danger zone! Overwrites the remote history with your local one. Remote commits may get lost!
  • 126.
    basics stages branchmerge remotes advanced When that merge came in like a wrecking ball $ git merge --abort $ git rebase --abort Abort an initiated merge or rebase
  • 127.
    basics stages branchmerge remotes advanced Git tag $ git tag Show all tags $ git tag -a v2.0 Tag the current commit with an annotation Tag (mark) important points in the commit history, e.g. when a working version is released $ git push origin --tags Push tags to the remote
  • 128.
    basics stages branchmerge remotes advanced Ignoring files Create a file in your repository (and add & commit it) named .gitignore, containing paths and rules that tell git which files to ignore. # Maven files target/ bin/ # Eclipse project files .project .classpath .settings # Mac OS .DS_Store # IntelliJ IDEA files *.iml *.ipr *.iws .idea # backup-files *~
  • 129.
    basics stages branchmerge remotes advanced Writing good commit messages
  • 130.
    basics stages branchmerge remotes advanced Writing *bad* commit messages
  • 131.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix”
  • 132.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“
  • 133.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“ “changes”
  • 134.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“ “changes” “it works!”
  • 135.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“ “changes” “it works!” “final commit”
  • 136.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“ “changes” “it works!” “final commit” “Testing in progress ;-)”
  • 137.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“ “changes” “it works!” “final commit” “Testing in progress ;-)” “TODO: write meaningful commit message”
  • 138.
    basics stages branchmerge remotes advanced Writing *bad* commit messages ”fix” “:(:(“ “changes” “it works!” “final commit” “Testing in progress ;-)” “TODO: write meaningful commit message” “Your commit is writing checks your merge can't cash”
  • 139.
    basics stages branchmerge remotes advanced Writing good commit messages Write commit messages as if you're giving commands to the codebase
  • 140.
    basics stages branchmerge remotes advanced Writing good commit messages Write commit messages as if you're giving commands to the codebase “Add DAO interfaces for Entities“
  • 141.
    basics stages branchmerge remotes advanced Writing good commit messages Write commit messages as if you're giving commands to the codebase “Add DAO interfaces for Entities“ “Implement basic version of AddressDAO”
  • 142.
    basics stages branchmerge remotes advanced Writing good commit messages Write commit messages as if you're giving commands to the codebase “Add DAO interfaces for Entities“ “Implement basic version of AddressDAO” “Fix bug in delete method of UserDAO” “Move package security to at.ac.tuwien.service” “Add generated fxml files for UI”
  • 143.
    basics stages branchmerge remotes advanced
  • 144.
    basics stages branchmerge remotes advanced
  • 145.
    Other great tutorials ● OfficialGit Documentation http://git-scm.com/doc ● TryGit – An interactive Git tutorial http://try.github.io ● Git Immersion http://gitimmersion.com/ ● Atlassian Git Tutorials https://www.atlassian.com/git ● Git Cheatsheet – Command categorisation http://ndpsoftware.com/git-cheatsheet.html ● LearnGitBranching http://pcottle.github.io/learnGitBranching
  • 146.
    Further reading ● A successfulGit branching model http://nvie.com/posts/a-successful-git-branching-model/ ● Changing history, or How to Git pretty http://justinhileman.info/article/changing-history ● Reset Demystified http://git-scm.com/blog/2011/07/11/reset.html ● Avoiding Git Disasters: A Gory Story http://randyfay.com/node/89 ● A Rebase Workflow for Git http://randyfay.com/node/91
  • 147.
  • 148.
    fin Thanks for listening- Enjoy git! Feedback appreciated thomas.rausch@tuwien.ac.at