Training

This training material is the Markdown used to generate training slides which can be found at End User Slides through it's RevealJS project.

Git Intro

What is a Version Control System (VCS)

Short Story of Git

What is Git

Getting Help

Git Setup

Workshop Time!

Setup

Configure

git config --global user.name "Your Name"
git config --global user.email you@example.com
git config --global --list

Workspace

mkdir ~/development
cd ~/development

-or-

mkdir ~/workspace
cd ~/workspace

Git Basics

Git Workflow

GitLab

New Project

Git and GitLab basics

  1. Edit edit_this_file.rb in training-examples
  2. See it listed as a changed file (working area)
  3. View the differences
  4. Stage the file
  5. Commit
  6. Push the commit to the remote
  7. View the Git log
# Edit `edit_this_file.rb`
git status
git diff
git add <file>
git commit -m 'My change'
git push origin master
git log

Feature Branching

  1. Create a new feature branch called squash_some_bugs
  2. Edit bugs.rb and remove all the bugs.
  3. Commit
  4. Push
git checkout -b squash_some_bugs
# Edit `bugs.rb`
git status
git add bugs.rb
git commit -m 'Fix some buggy code'
git push origin squash_some_bugs

Merge Request

Merge request example

Feedback and Collaboration


Merge Conflicts

Example Plan

  1. Checkout a new branch and edit conflicts.rb. Add 'Line4' and 'Line5'.
  2. Commit and push
  3. Checkout master and edit conflicts.rb. Add 'Line6' and 'Line7' below 'Line3'.
  4. Commit and push to master
  5. Create a merge request and watch it fail
  6. Rebase our new branch with master
  7. Fix conflicts on the conflicts.rb file.
  8. Stage the file and continue rebasing
  9. Force push the changes
  10. Finally continue with the Merge Request

Example 1/2

git checkout -b conflicts_branch

# vi conflicts.rb
# Add 'Line4' and 'Line5'

git commit -am "add line4 and line5"
git push origin conflicts_branch

git checkout master

# vi conflicts.rb
# Add 'Line6' and 'Line7'
git commit -am "add line6 and line7"
git push origin master

Example 2/2

Create a merge request on the GitLab web UI. You'll see a conflict warning.

git checkout conflicts_branch
git fetch
git rebase master

# Fix conflicts by editing the files.

git add conflicts.rb
# No need to commit this file

git rebase --continue

# Remember that we have rewritten our commit history so we
# need to force push so that our remote branch is restructured
git push origin conflicts_branch -f

Notes

Revert and Unstage

Unstage

To remove files from stage use reset HEAD. Where HEAD is the last commit of the current branch:

git reset HEAD <file>

This will unstage the file but maintain the modifications. To revert the file back to the state it was in before the changes we can use:

git checkout -- <file>

To remove a file from disk and repo use git rm and to remove a directory use the -r flag:

git rm '*.txt'
git rm -r <dirname>

If we want to remove a file from the repository but keep it on disk, say we forgot to add it to our .gitignore file then use --cache:

git rm <filename> --cache

Undo Commits

Undo last commit putting everything back into the staging area:

git reset --soft HEAD^

Add files and change message with:

git commit --amend -m "New Message"

Undo last and remove changes

git reset --hard HEAD^

Same as last one but for two commits back:

git reset --hard HEAD^^

Don't reset after pushing

Reset Workflow

  1. Edit file again 'edit_this_file.rb'
  2. Check status
  3. Add and commit with wrong message
  4. Check log
  5. Amend commit
  6. Check log
  7. Soft reset
  8. Check log
  9. Pull for updates
  10. Push changes
# Change file edit_this_file.rb
git status
git commit -am "kjkfjkg"
git log
git commit --amend -m "New comment added"
git log
git reset --soft HEAD^
git log
git pull origin master
git push origin master

git revert vs git reset

Reset removes the commit while revert removes the changes but leaves the commit Revert is safer considering we can revert a revert

# Changed file
git commit -am "bug introduced"
git revert HEAD
# New commit created reverting changes
# Now we want to re apply the reverted commit
git log # take hash from the revert commit
git revert <rev commit hash>
# reverted commit is back (new commit created again)

Questions

Instructor Notes

Version Control