- Table of Contents
- Housekeeping
- Introduction
- git init / git clone
- git status
- git add
- git commit
- git branch
- git checkout
- git merge
- git rebase
- git push
- Bonus
- Workshopt repo
- ~ 1 hour
- Ask questions. Don’t worry about interrupting the workshop.
- I’ll also remind myself to stop and ask if you have any question.
- Be sure to have access to git.
For the peace of mind.
Definitions:
- Workspace - all unstaged changes.
- Staging area (a.k.a. index) - a “holding area” for changes that will be committed when you next do git commit.
Further Readings:
Getting and Creating Projects.
init: Create an empty Git repository or reinitialize an existing one.clone: Clone a (remote) repository into a new directory
$ mkdir new-repo
$ cd new-repo
$ git init
# Go to a different working directory assuming you are still in the new-repo
# directory.
$ cd ..
# if you have ssh setup already, do:
$ git clone [email protected]:dawranliou/git-workshop.git
# otherwise, do:
$ git clone https://github.com/dawranliou/git-workshop.git
Show the working tree status.
Git status gives you a lot of information about the current state of your git repo. When I screwed up a git repo, I usually could deduce what went wrong based on the git status. You should be doing git status a lot too to get familiar with git.
$ cd git-workshop
$ git status
Add file contents to the index.
This is the first stage of the 2-stage commit. We can have multiple dirty files, i.e. files with untracked changes, but only select a couple to be committed.
# Create a new empty file
$ touch excercise.txt
$ git add excercise.txt
# Or add everything to the staging area
$ git add .
$ git status
Record changes to the repository.
A commit is a single unit in git’s version control system. Every single commit is assigned with a SHA code to preserve its integrity. Traversing the commit history and operating on the commit history is simple and fast in git. You are encouraged to make small, incremental commits rather than a huge one. You are also encouraged to make imperfect commits and clean up the history later.
$ git commit
# Or attach the commit message in one go
$ git commit -m "Add file - excercise.txt"
$ git status
$ echo "# excercise.txt - a file for excercise" >> excercise.txt
$ git add excercise.txt
$ git commit "Add file comment"
List, create, or delete branches.
Branch is one of the most liberating ability given by a version control system. You are free to track multiple states of your source code at any given time without worrying about screwing it up.
In practice, I rarely use the branch command to create a new branch because of
a shortcut we’ll see in the next section.
Switch branches or restore working tree files
$ git checkout -b develop
$ git status
$ echo "This is line one" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the first line"
$ echo "This is line two" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the second line"
$ echo "This is line three" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the third line"
$ git status
$ git checkout master
$ echo "This is line 0" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the zeroth line"
$ echo "This is line 1" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the first line"
$ echo "This is line 2" >> excercise.txt
$ git add excercise.txt
$ git commit "Add the second line"
Join two or more development histories together
When we have two versions of our source code, we can then merge them together. A
practical case would be merging a feature branch into the master/main branch
after the feature branch is fully tested. Imaging we currently have this:
A---B---C develop
/
D---E---F---G master
By merging the develop to master, our commit history looks like:
A---B---C develop
/ \
D---E---F---G---H master
# Merge command merges the "other" branch into the "current" branch. Therefore,
# we need to switch back to the master branch first.
$ git checkout master
# Merge in the develop brach
$ git merge develop
# ...and a merge conflict!
# Open your text editor and fix the conflicts
$ git merge --continue
# Or abort it
# $ git merge --abort
Reapply commits on top of another base tip.
So far, we haven’t encounter any command that is destructive, which isn’t the
case for rebase. rebase rewrites the git history and it is irreversible, so
use it cautiously. Although you can choose to abort the rebase operation
anytime, it’s safest to first create a temporary branch to track the existing
state before rebase. However destructive, it is useful to keep the git history
clean while collaborating with others. Let’s think of the feature branch
again:
A---B---C feature
/
D---E---F---G master
When we rebase the feature branch on our master, we rewrite the history on
feature to:
A'--B'--C' feature
/
D---E---F---G master
The feature’s commits, A, B, and C, becomes A', B', and C'.
Update remote refs along with associated objects.
- Git official references
- Git aliases
- Oh Shit, Git!?! - how to fix your screwed-up git repository.