Fit for Git

A basic Git workshop

open http://ffg/ for handout

Recording Changes

First-time setup

git config --global user.name "Your Name"
git config --global user.email user@domain.tld

Git is a distributed system, so it uses email addresses to uniquely identify users.

  • try git help config

Creating a repository

git init repo
cd repo

This creates a new, empty repository which lives only on your computer (for now).

echo git > cool_stuff.txt
git status
  • run git init without argument to start tracking existing files in ./

Staging changes

git add cool_stuff.txt
git status

  • you can also add entire directories with git add
  • make changes in different parts of a file and try git add -p

Committing (to) a version

git commit
git status

  • you can skip git add for files known to Git using git commit -a

Viewing history

gitk GUI separate tool
tig ncurses separate tool
git log text included in git

Every commit contains a snapshot of the entire repository; unlike checkins in RCS or Subversion, which operate on individual files or directories, respectively.

The why of commit messages

  • the who/when/what of a change is recorded automatically
  • commit messages primarily explain the “why”
  • they summarize changes for history overviews (e.g. in gitk)
  • they may contain pointers to ticket IDs for future reference

The how of commit messages

First line: very short summary, what changed?

After a blank line follows the answer to the "why". What was
the motivation for the change? How is the need addressed by
this commit?

If applicable: Which alternatives were considered, and why
were they discarded?

Closes #bugid.

Training Time

git config --global user.name "Your Name"
git config --global user.email user@domain.tld
  1. create a new, empty repository
  2. commit some changes
  3. view the resulting history
  • Try git revert HEAD~1. What does it do? Can you revert other changes?
  • Look at the contents of the .git directory - what changes at each step?

Central Repository

Clones

git clone git@ffg:ffg/hello.git
cd hello
ls
git status

This has created a clone of the hello.git repository. The repository we cloned from is called its origin.

  • By convention, central (“bare”) repositories have the suffix .git, while repositories we can make commits in do not.

Pulling updates

git pull
*   8c577b0 Merge branch 'master' of ↵
            http://ffg/gogs/ffg/hello.git
|\  
| * b983d4c Linux is also cool
* | e8fa16e Start collecting numbers
|/  
* 3adcc80 My second git commit
* b323f69 My first git commit
  • try git log --graph --oneline

Interpreting conflicts

git pull

In cool_stuff.txt:

Tübix
Git
<<<<<<< HEAD
reveal.js
=======
Linux
>>>>>>> b983d4cc25261780d3b2a21a142d4750394c5c77

Resolving conflicts

git mergetool
git commit

“Uploading” changes

git push
# ...
# To git@ffg:ffg/hello.git
#    d039e72..325b819  master -> master

git push
# ...
# To git@ffg:ffg/hello.git
#  ! [rejected]        master -> master (non-fast-forward)
# error: failed to push some refs to 'git@ffg:ffg/hello.git'
# ...

Training Time

  1. get a clone of
  2. add a new file to your clone
  3. upload your change and get the changes of other participants
  4. repeat with the file cool_stuff.txt (this should give conflits which you need to resolve)

Branching and Merging

Visualizing history

linear history
linear history
git log --graph --oneline

* 5567e85 (HEAD -> master) My third git commit
* df5a1e0 My second git commit
* d3a1a32 My first git commit

Branching off

two branches
two branches
git branch feature master~2
git checkout feature
$EDITOR; git commit -a

* 16ae900 (feature) Commit on feature branch
| * 5567e85 (master) My third git commit
| * df5a1e0 My second git commit
|/
* d3a1a32 My first git commit

Merging

merge
merge
git checkout master
git merge feature

Merge conflicts

Git merges histories of the entire repository; until all conflicts are resolved and committed, the merge is “in progress” for all files.

  • You can abort a merge in progress with git merge --abort

Rebasing

rebase
rebase
git checkout feature
git rebase master

Cherry-picking

cherry-pick
cherry-pick
git checkout master
git cherry-pick feature

Merge/Rebase/Cherry-pick

two branches
two branches
merge
merge
rebase
rebase
cherry-pick
cherry-pick

Pull Requests

Training Time

  1. open http://ffg/gogs/ffg/hello in a browser and fork the repository
  2. clone your fork and make some changes to it
  3. create a pull request from your changes

Rewriting History