Introduction to Version Control with Git

Table of Contents

1 Why version control?

  • know how files changed
  • know when files changed
  • undo unwanted changes
  • incremental documentation of changes

2 Why Git?

  • popular with developers
  • displacing other centralized version control systems
  • has some of the most sophisticated functionality (perhaps more than most people need)

3 A brief history of version control

  • file-at-a-time: SCCS, RCS
    • files introduced to control and managed one at a time
    • advisory locking to prevent simultaneous editing
    • work has to happen on one system in one place
  • centralized: CVS, Subversion
    • central, network-accessible repository
    • users have individual checkouts containing all working files, but usually only some repository metadata
    • typically manage entire directory trees instead of individual files
  • distributed: Git, Mercurial, BZR
    • all working files and repository metadata available locally
    • (possibly) multiple repositories
    • non-hierarchical relationships between repositories
    • changes can be (selectively) transferred between repositories

4 Git concepts

  • files editable in working directory tree
  • staging changes: group related changes for a commit
  • commit to repository: record snapshot of changes with a comment
  • change -> stage -> commit -> …
  • commit ID instead of version numbers
    • SHA-1 hash of commit data (40 hexadecimal digits)
    • can be abbreviated to any unique initial part of the hash (7 chars is common)
  • Various ways to transfer data between repositories
    clone
    make a copy of a repository
    push
    integrate changes from local repository into remote
    pull
    integrate changes from remote repository into local

5 Git commands

  • Every git subcommand has a man page: "git commit" -> "man git-commit", also "git help commit", "git commit –help"
  • "git –help" lists common subcommands

5.1 git init

create empty git repository in current (or specified) directory

5.2 git config

change git configuration variables

  • some basic options
    • git config user.name "Your Name"
    • git config user.email "your@email.address"
    • git config core.editor "your-favorite-editor"
  • (maybe) create .gitignore file specifying file patterns to automatically exclude from tracking
  • per-repo ("–local") options in ".git/config", per-user ("–global") in "$HOME/.gitconfig", system-wide ("–system") in "/etc/gitconfig"

5.3 git add

indicate files to be staged for commit

git add new.c
stage new files
git add edit.c
stage modified files

The set of staged files is also called "the index" in documentation

5.4 git commit

commit all staged changes to repository

  • you'll be prompted for a commit message, try to make it meaningful!

5.5 git status

show status of new/modified/staged files

5.6 git log

log of all commits

5.7 git diff

show differences between modified files and committed versions

5.8 git rm

remove a file from the working directory and maybe also staging (previous commits stay in repository)

5.9 git mv

rename a file (commit history moves with it)

5.10 git reset

this command is arguably heavily overloaded

  • unstage changes
  • revert working files to committed versions
  • undo commits

5.11 git clone

make local copy of remote repository

5.12 git remote

show and manage remote repositories connected to your local one

5.13 git pull

integrate changes from remote repository

  • actually equivalent to "git fetch; git merge"

5.14 git push

apply your changes to a remote repository

  • never use "git push –force"!

6 using git in this class

  • repository for Puppet configuration and modules
  • clones by each group member
  • simple cycle
    • "git pull" (sync with remote repo)
    • edit/create/delete files as needed
    • "git add <changed files>"
    • "git commit"
    • "git push" (put your changes in Puppet repo)
  • if "git push" fails…
    • "git pull" again,
    • resolve merge conflicts (if needed),
    • "git push" again
  • Puppet config directory will get a read-only clone of Puppet repo

7 More Git resources and tutorials

Author: Steve VanDevender

Created: 2022-04-20 Wed 12:07

Validate