Using Git for Revision Control
Introduction revision history
Git is a distributed revision control system. Its purpose is to master
provide a structured way to track changes to files as well as branches
manage contributions from many collaborators.
The Nouns of Git Create project
Repository Revision-controlled directory. Inside a "repo", featureA
the hidden .git folder contains metadata about the project, its
upstream source, and all the changes that have been made to Add function A Add featureA
it. The distinction between Git and centralized revision
control systems is that each developer uses Git to make a
local copy of the "remote" (upstream or official repo), makes
their changes, and then records them using commits. Fix bug #1
commits
Synchronization between the remote and everyone's local
copy allows each developer's changes to propagate to their
peers.
Add help text
Commit The fundamental unit of a revision control system.
A snapshot of a repository at a specific time, annotated with a
log entry ("commit message") that describes the changes Merge featureA Fix bug in featureA
made since the last commit. into master
Branch A timeline or series of commits. The mainline
branch is usually called the "master" or "trunk." Branches
can diverge from the trunk, creating an alternate timeline to
develop a new feature without touching the stable code.
These changes can later be merged into the main branch.
Workspace The project’s working directory. The files in the
workspace reflect the state of the current branch as of the last
commit, plus any changes since then. Untracked files are not
associated with a particular branch, and show up in all of .git workspace
them.
Index Staging area for commits. Adding a file to the index bigProject index: files with
caches its changes and tells Git to snapshot it the next time a
commit is made. Changes not staged in the index are not changes staged for commit
committed.
file with
changes that aren’t going
untracked file to be committed
unchanged file
CC-BY Alex Krolick
Using Git for Revision Control
The Verbs of Git >> git clone git@github.com:user/project.git clone
git <command> <-flags> <inputs>
>> cd project
git checkout –b featureA git@github.com: project
>> ls
add <files> stage files for commit file1 user/project.git
file2
mv <oldName> <newName> rename a file, preserving its
history at the old name. Index the change.
rm <file> delete a file, recording the change in the index. >> git checkout –b bugfixBranch branch
>> touch file3 checkout
commit –m “<message>” commit indexed changes >> echo “new code” >> file1
branch <name> create a new branch >> git status
modified: file1
checkout <name> switch to branch untracked files: file3
>> git add .
merge <branch> play back commits from “branch” onto the modify
>> git status
current branch
modified: file1 add
rebase <parent> rewind commits on current branch, add new file: file3
commits from the parent since the branches diverged, then >> git commit –m “fixed stuff”
add changes back in
revert <commit> undo commit
>> git checkout master
reset –-hard roll back changes since commit >> git diff bugfixBranch commit
modified file1
stash save changes for later, reverting edits
+fixed stuff merge
stash pop restore stashed changes created file3
>> git merge bugfixBranch
status show list of changes and untracked files
log –p pretty-print commit log push
>> git push origin master
diff show line-by-line differences between files in the
workspace and last commit
git@github.com: project
diff --cached show line-by-line differences between staged >> git request-pull bigProjectv1.0 \ https://github.com/user/project user/project.git
files and last commit master \
> pullrequest.txt
init create empty git repository
clone <remote url> copy a remote repository
fetch <remote> get changes from remote branch
pull fetch and merge changes from remote branch More Resources
push publish local commits to remote Code hosting & collaboration git-scm.com/doc
request-pull generates a patch message that can be sent to github.com
bitbucket.org
others describing your changes and where they can be
downloaded
Tutorials & cheatsheets
atlassian.com/git
Official documentation
CC-BY Alex Krolick