slides12
slides12
Practical
System
Skills
Fall 2019 edition
Leonhard Spiegelberg
[email protected]
14
CS6 Practical System Skills
Fall 2019
Leonhard Spiegelberg [email protected]
Official git book: https://git-scm.com/book/en/v2
Cheatsheets:
https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf
https://about.gitlab.com/images/press/git-cheat-sheet.pdf
https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet
⇒ automated versioning
- source files
- build files / make files
6 / 53
⇒ There are multiple version control systems, popular are
subversion
mercurial
7 / 53
Created by Linus Torvalds in 2005
⇒ designed for linux kernel development
→ https://www.youtube.com/watch?v=4XpnKHJAok8
9 / 53
Basic syntax:
10 / 53
Before running commands, we need to specify a user and email
address under which changes are tracked.
11 / 53
Repository = a virtual storage of your project.
Note: You can also use git config --local, which is the same as git config.
13 / 53
⇒ When working in git, you edit your local files in your local
working copy (aka local repository)
checkout files
stage files
commit files
16 / 53
⇒ use git add to add files to the staging area
Syntax:
⇒ after you added a file, you can print the changes via
git status -v or git status -vv
⇒ After you staged all your files, you can create a commit via
git commit -m "commit message"
18 / 53
tux@cs6demo:~$ mkdir repo && cd repo && git init
(1) create repo
Initialized empty Git repository in /home/tux/repo/.git/
tux@cs6demo:~$ git config user.name "tux"
tux@cs6demo:~$ git config user.email "[email protected]" (2) set repo-specific user
tux@cs6demo:~$ echo "Hello world" > README.md
tux@cs6demo:~$ git status
On branch master (3) untracked new file in
working directory
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
tux@cs6demo:~$ git add README.md
tux@cs6demo:~$ git status (4) git add stages the file
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
Tip: you can set an alias for a command in git, i.e. to have git unstage file instead of git
reset -- file , use git config --global alias.unstage 'reset --'
20 / 53
⇒ Besides adding files, removal is tracked too.
1. git rm file
2. rm file && git add file
21 / 53
⇒ Sometimes you have files in your directory, which you don't
want to track
⇒ Often only part of the hash is shown, part of it or the full hash
can be used to reference a specific commit.
25 / 53
To undo a commit there are two options:
⇒ use git revert. Only use git reset if you know what you're
doing.
26 / 53
⇒ commits form a directed acyclic graph(DAG) with branches,
i.e. each node is a commit. Often (incorrectly) referred to as commit tree.
HEAD
HEAD
Branch A
branch merge
Branch B
27 / 53
To work with the DAG, there are multiple commands:
branch out, i.e. create a new branch.
git branch
Visually it is "forking" the DAG.
29 / 53
current commit
⇒ You can create a new branch via
30 / 53
⇒ you can checkout a branch or commit, i.e. replace the files in
your working directory with the ones belonging to this commit
32 / 53
⇒ sometimes you want to see an old version of your work, you can
do so by checking out a commit via its hash (or parts of it)
33 / 53
⇒ You can also checkout a commit relative to the most recent one,
for this do git checkout HEAD~n
HEAD
⇒ Example: git checkout HEAD~1
34 / 53
⇒ to join two branches (or commits), git provides two mechanisms:
37 / 53
⇒ Rebase "replays" changes on top of the branch to rebase on
⇒ Merge creates a new merge commit
⇒ More on differences/use cases next lecture
rebasing merging
38 / 53
git has an automatic merging algorithm, which often does a good job.
39 / 53
⇒ to merge a branch feature into a branch master do
or as one-line version:
git merge master feature
40 / 53
git init
echo -e "README\n----" > README.md
git add . && git commit -m "initial commit" merge
git checkout -b feature commit
echo "This is the one and only true README
from feature branch" >> README.md
git commit -a -m "feature readme update"
git checkout master
echo "Only the master branch has the wisdom
to write a README" >> README.md feature master
git commit -a -m "master README update" readme README
update update
After all conflicts are resolved, create a merge commit via git
commit.
⇒ for this remotes are used, i.e. servers which host a remote
version of the repo
Remote
pull repo pull
push push
local local
repo repo
45 / 53
⇒ remote repository are typically hosted in the cloud
Github (Microsoft)
Bitbucket (Atlassian)
GitLab (GitLab Inc.)
46 / 53
git remote add <remote_name> <remote_repo_url>
Example:
git remote add origin https://github.com/cs6tux/lec12.git
⇒ you can also clone the remote repository to get a local copy via
git clone. Note: You can clone via password
based authentication OR use SSH keys!
⇒ SSH keys allow you to work faster!
51 / 53
52 / 53
Exam next Tue, 4pm-5:20pm @ CIT 477