Git resources

This is a collection of online resources for those learning Git.


Setup

  • Remote Git server (e.g. GitHub, Bitbucket)
  • Account with (shared) username
  • Local install
  • Git config with own name

Running git config sets up your local git client with stateful data, which in turn is applied to each commit.

git config --global user.name 'Lightenna training'
git config --global user.email 'lighttrain@users.noreply.github.com'

What is git?

  • Distributed version control system
  • The ‘master’ copy can be anywhere
  • Conventionally, we adopt a single remote
  • All contributions pushed to that remote
  • Demonstrate with an example

File tree

  • /home/myuser/devops-workstream/
    • kubernetes
      • service.yaml
      • deployment.yaml
    • terraform
    • vagrant
    • README.md

Git helps us manage sets of files.

This example set is taken from the open-source Devops-Workstream repo.


Changes

  • /home/myuser/devops-workstream/
    • kubernetes
      • service.yaml
      • deployment.yaml
    • terraform
    • vagrant
    • README.md

When we change a file, we need everyone else to see those changes.

We could simple copy the files around, but that risks overwriting others’ changes.

Git provides a mechanism for us to contribute those changes, while managing the risk of overwriting other changes.


Past: CVS/Subversion

sequenceDiagram participant Working copy participant Remote repo Remote repo ->> Working copy: svn checkout Remote repo ->> Working copy: svn update Working copy ->> Remote repo: svn commit
  • svn checkout - get a working copy
  • svn update - get latest changes
  • svn commit - push my changes

Git pull (simplified)

sequenceDiagram participant Working copy participant Local repo participant Remote repo Remote repo ->> Working copy: git clone Remote repo ->> Working copy: git pull
  • git clone - create local repo and working copy
  • git pull - update repo and working copy

Git pull

sequenceDiagram participant Working copy participant Local repo participant Remote repo Remote repo ->> Working copy: git clone Remote repo ->> Working copy: git pull Remote repo ->> Local repo: git fetch Local repo ->> Working copy: git checkout
  • git clone - create local repo and working copy
  • git pull - update repo and working copy
  • git fetch - update local repo only
  • git checkout - update working copy only

Local repo stored in .git

  • /home/myuser/devops-workstream/
    • .git
    • kubernetes
      • service.yaml
      • deployment.yaml
    • terraform
    • vagrant
    • README.md

Pull process

  • git pull
    • Combines fetch and checkout
      • Fetch from remote
      • Update local repo
      • Update working copy

Git push (simplified)

sequenceDiagram participant Working copy participant Local repo participant Remote repo Working copy ->> Local repo: git commit . Local repo ->> Remote repo: git push
  • git commit . - stage and commit to local
  • git push - push local to remote

Git push (including stage)

sequenceDiagram participant Working copy participant Stage participant Local repo participant Remote repo Working copy ->> Stage: git add Stage ->> Local repo: git commit Local repo ->> Remote repo: git push
  • git add - stage file for commit
  • git commit - commit to local
  • git push - push local to remote

Commit and undo (Visual Git Reference)

Git add, commit, reset and checkout operations, Source: A Visual Git Reference

Push process

  • git commit .
    • Combines git add and git commit
      • Stage for commit
      • Commit to local repo
  • git push * Push local repo to remote

Exercise: clone remote

  • Clone remote repo locally (git clone <repo_address>)
  • Change README.md, edit a random line
  • Stage and commit (git commit . -m "message")
  • Push (git push)
  • Check online

Create a local branch

  • Done from a source branch
  • git checkout -b newbranch
gitGraph: commit branch newbranch checkout newbranch commit checkout master commit checkout newbranch commit checkout master commit

Push to remote

  • So far only local
  • git push origin newbranch
  • git push --set-upstream origin newbranch

Merge back

  • Make changes to new branch
  • git checkout newbranch
  • Edit, commit (git commit .)
  • git checkout master
  • git merge newbranch
gitGraph: commit branch newbranch checkout newbranch commit checkout master commit checkout newbranch commit checkout master commit merge newbranch

Branches as chains of commits

Source: A Visual Git Reference

Exercise: create repo

  • Create remote repo
  • Clone locally
  • Copy files into working directory
  • Stage
  • Commit
  • Push
  • Check online

Rollback

Reset operation showing rollback of last 3 commits, Source: A Visual Git Reference

Exercise: undo a bad commit

  • Clone locally (already done)
  • Mess up a file
  • Stage and commit
  • View history
  • Checkout a previous commit
    • Move HEAD back to previous commit
  • Make rational change
  • Stage and commit again
  • Push
  • Check history online

Exercise: dump local, fetch remote

  • Clone locally (already done)
  • Mess up a file
  • Stage and commit
  • View history
  • Fetch remote
  • Reset local
  • View history again

Exercise: merge feature branch

  • Clone locally (already done)
  • Create branch
  • Checkout branch
  • Make changes
  • Stage and commit
  • Push (optional)
  • Switch to (checkout) master
  • Merge
  • Push

Exercise: tag a branch for release

  • Clone locally (already done)
  • Checkout master
  • Check latest commit ID
  • Create tag
  • Update files
  • Stage and commit
  • Checkout tag

Basics of tagging


Exercise: group collaboration

  • Clone shared repo
  • Assign ticket numbers
  • Create feature branch
  • Add unique file
  • Stage and commit
  • Switch to (checkout) master
  • Merge
  • Push

View as a presentation

Leave a comment