22.
Exercise - Setting up a repository (git)
Initialize a fresh repository starting from your favorite project
(or create a new one)
$ cd project
$ git init
Initialized empty Git repository in .git
Add a snapshot of the working tree to the index (temporary staging area)
$ git add .
Persist the contents of the index into the repository:
$ git commit
This will prompt you for a commit message.
You just stored the first version of your project in the repository.

23.
Exercise - Setting up a repository (EGit)
Initialize a fresh repository starting from your favorite Eclipse project
In package explorer select the project(s) you want to version
Note: if you select multiple projects they must all reside under a common
folder
Team > Share Project > Git, click "Next"
Select the project(s) to be shared
Click "Create Repository", then "Finish"
Team > Add to Version Control
adds the selected files to the index
Team > Commit
opens the commit dialog prompting for a commit message
Click "Commit"
You just stored the first version of your project in the repository.

24.
Exercise - Making Changes (git)
Modify or add some files ... and stage them for commit
$ git add file1 file2
to get a brief summary of the situation run
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1
# modified: file2
to compare working tree and index run
$ git diff
to compare index and last commit run
$ git diff --cached
then commit all staged changes
$ git commit

25.
Exercise - Making Changes (EGit)
Modify or add some files ... and stage them for commit
Team > Add to Version Control
adds the selected files to the index
Resources are decorated according to their state
following these preferences:
Team > Git > Label Decorations
General > Appearance > Colors and Fonts > Git
To compare working tree and index select
Compare with > Git Index on modified resources
Team > Commit
and provide a commit message describing your change

26.
Exercise - Browsing History (git)
Inspect the history using
$ git log
if you also want to see the diffs use
$ git log -p
for an overview use
$ git log --stat --summary
if you prefer a graphical display of the history run
$ gitk
or to display all branches
$ gitk --all

27.
Exercise - Browsing History (EGit)
Inspect the history by selecting
Team > Show in Resource History
from the context menu
Using the toggle buttons of the history view you may adapt its filtering
behavior to your needs.
Select a commit to see what changes it introduced.
Comparing file revisions
Select a file in the package explorer ...
... and select a commit from the commit log pane
then select Compare with working tree from the context menu
... or select two commits you want to compare
then select Compare with each other from the context menu.

28.
Exercise - Branching (git) - 1
Branches are used heavily with git to isolate development
on different topics from each other.
To create a new branch to e.g. implement a new feature run
$ git branch newfeature
list all branches( the currently checked out branch is marked with * )
$ git branch
newfeature
* master
check out this branch to your working tree
$ git checkout newfeature
then do some changes and commit them
$ git checkout -b newfeature
is a shortcut combining both these steps in one go

29.
Exercise - Branching (git) -2
Switch back to the master branch
$ git checkout master
do some different changes on the master branch and commit them.
At this point the two branches have diverged with different changes in each.
To merge the feature into the master branch run (from the master branch)
$ git merge newfeature
In case of conflicts markers are inserted into the conflicting files
this can be shown with
$ git diff
edit the files with conflicts to resolve the conflicts and commit the
resolving changes.
Inspect the resulting version history using
$ gitk
Then delete the feature branch
$ git branch -d newfeature

30.
Exercise - Branching (EGit)
Repeat the previous exercise in EGit, use the following context menu on your
Eclipse project
Team > Branch
to create branches and to switch between different branches.
Merging is not yet available in EGit (we are working on it)
hence for now use C git for merging.

31.
Exercise - SSH Configuration
SSH Keys
check if your ~/.ssh already contains SSH keys, if not run (use your email)
$ ssh-keygen -t rsa -C "joe@example.com"
id_rsa is the default private key, id_rsa.pub is the default public key
choose a good pass phrase to protect your key
In Eclipse check that you point at the right keys
(you may also generate them from here)
Preferences > General > Network Connections > SSH2

32.
Gerrit Registration
To demonstrate collaboration using git and Gerrit we invite you to play
contributor on the simple RCP mail example
Follow registration instructions in EGit contributor guide
Add a remote for the review server
Start Eclipse 3.5 or 3.6
The workflow we cover in this tutorial is the exact workflow that you would use if
you wanted to contribute to the EGit and JGit projects

34.
Exercise - Collaborating (git) - 2
push your change back (from local topic branch to Gerrit)
$ git push review
this pushes back all changes which are on the current branch
but not in remote master
Go to Gerrit and review the change you just uploaded
http://egit.eclipse.org/r/#mine

35.
EMail based collaboration
create patch file (for last commit)
$ git format-patch -1
... attach patch file to Bugzilla
... or send it to mailing list for review
... or directly to another developer
... as receiver apply patch file to another repository
$ git am <patch-file>
check that the patch has been applied to your current branch
$ git log -p
Generating patch for attachment to Bugzilla

39.
Rebase
Rewrite your project history!
You want to base your work on the latest version
Upstream changed while you work
You haven't published yet
Solution: Reapply every change onto the latest upstream.
Rebase automates this
on the branch you want to rebase onto latest upstream run
$ git fetch [origin]
$ git rebase origin/master
Rebase may lead to conflicts

40.
Rebasing (interactive)
A tool for cleaning up history before sharing your version
- change something a
- change something else b
- oops, not so good, fix a
- changed my mind about b
- Shawn didn't like some piece of a
rebase into
- Change something a
- Change something else b
Start with the last commit you want to retain as-is:
git rebase -i
<after-this-commit>
then follow section "Interactive Mode" of git-rebase reference
Now other people can understand your work better