<PREV>
<INDEX> <NEXT>
Version
Control
Why versioning systems?
- keep history: revert changes
- allow collaboration: merging and branching
- applications (text based): software
projects, PhD thesis,
papers (LaTex - overleaf.com), resume, websites
History and comparison: (cvs -> svn -> git) action
arguments
- philosophy (store differences vs store
snapshots)
- storage (local -> server ->
distributed)
- access control
- operation (local vs server)
- file states: committed (saved index, files;
pointed by HEAD)
- -> modified (files changed
locally)
- -> staged (index modified)
- -> commited (index, files saved to new
commit)
![GIT structure](gitstructure2.png)
### Getting started
# Getting help:
git help <command>
git <command> -h
man git-<command>
# New repository
cd $REPO_DIR ; git init ; git add
<dirs/files> ; git commit -m 'initial blurb'
# Existing repository
cd $REPO_DIR ; git clone <URL>
# Control what's ignored
# templates at https://github.com/github/gitignore
emacs $REPO_DIR/.gitignore
# Customize (layered)
/etc/gitconfig
~/.config/git/config
emacs $REPO_DIR/.git/config
git config
### Making changes
# add/modify files/directories
- stage modified files: git add <dirs/files>
- commit staged files: git commit -m 'comment'
- commit all changes skipping staging: git commit -a -m 'comment'
# getting info
- what was modified or added: git status
- what was changed but not staged: git diff
- what was changed and staged: git diff --staged
# deleting files/directories
- remove the file from the filesystem
and from git: git rm <file>
- remove the dir from the filesystem
and from git: git rm <dir>
- remove the files only from git: git rm --cached
- move files: git mv
<oldfile> <newfile>
# searching
- find tracked files matching pattern: git grep <regexp>
- find commit comments matching pattern: git log -grep=<regexp>
### Handling commits
- view history (different options for
details):
git log --pretty=format:"%h %s"
--graph
- where are we? (HEAD)
# Undo things
- replace LAST commit: git
commit -amend
- unstage a file: git
reset HEAD <file>
- move HEAD (soft/mixed/hard/merge/keep
reset):
git reset --soft/mixed/hard/merge/keep <commit>
- unmodify file/dir (from previous commit): git checkout <commit> --
<file/dir>
- revert patches of old commits (records new
commits): git revert <commit>
# Branching:
- list branches: git
branch -v
- create new branch: git
branch <name>
- switch branches (restore files and modify
HEAD)): git checkout <branch>
- create new branch and switch
(=branch+checkout): git checkout -b
<branch>
- delete branch: git
branch -d <branch>
- differences between branches: git diff
<branch1>..<branch2>
# Merging into the current branch: git
merge <branch>
# merge is not symmetric: files the same but
pointers different
- fast-forward merge = moves the pointer (--ff-only)
- merge commit = new commit having two
ancestors (--no-ff)
- conflicts detected: git
mergetool
# Stashing: save some changes not ready for commit
- stash changes: git
stash save "Message"
- list the stash: git
stash list
- show an item from stash: git stash show -p <stash>
- retrieve from stash: git
stash pop/apply <stash>
- delete from stash: git
stash drop <stash>
- clear the stash: git
stash clear
# Rebasing (apply patches from a different branch):
- apply patches from the current branch to
the tip of another one:
git rebase <branch>
- apply patches between branch1 and branch2
to branch3:
git rebase --onto
<branch3> <branch1> <branch2>
# should be followed by fast-forward
merge
- apply patches from one commit to a branch:
git cherry-pick <commit>
![merging](merge.png)
### Collaborating with
others
# working with remotes
- view remotes: git
remote -v
- add remote and create a remote tracking
branch: git remote add <remote>
<URL>
- rename remote: git
remote rename <oldremote> <newremote>
- remove remote: git
remote remove <remote>
- view details about a remote: git remote show <remote>
- fetch from remote into the remote tracking
branch: git fetch <remote>
- pull from a remote to current branch
(=fetch+merge): git pull
<remote>
- push from a branch to the remote: git push <branch> <remote>
# managing tracking branches
- track master from remote and copy to local
master: git clone <remote>
- track other branch from remote: git checkout --track
<remote>/<branch>
### Best practices
- work with remotes: fetch/merge
before push
- local: branch synchronization (merge
often, track changes to master)
- local: commits small and focused
with informative comments
- rebase: do not rebase commits that
were pushed
- reset: save current log to remember
commit SHAs
- merge, revert: clean working
directory first (commit all changes)
- files: multiple files,
specialized
- files: meaningful names without
spaces, symbols, or foreign characters
- files: keep lines short (< 80
char)
Further lecture:
Python Quiz 2
Python Quiz 2
(Chap 7-11)
<PREV> <INDEX>
<NEXT>