Git and Github: Cs 4411 Spring 2020
Git and Github: Cs 4411 Spring 2020
CS 4411
Spring 2020
If that doesn’t fix it, git.txt contains the phone number of a friend of mine who understands git. Just wait through a few minutes
of “It’s really pretty simple, just think of branches as...” and eventually you’ll learn the commands that will fix everything.
Outline for Today
• Git overview
• Git vs. GitHub
• Basic Git commands
• Conflicts and merges
• Branches
• Recovering from errors
Git Model
Remote repo
Head Commit C:
/src/grass/process.c
/src/grass/process.h
Commits A B C /src/apps/mt.c
A B C D
Origin push
Local repo
Head Commit D:
/src/apps/mt.c
/src/apps/myprogram.c
A B C D
/src/make/Makefile.apps
Git with GitHub
GitHub repo: etremel/egos GitHub repo: jsmith/egos
Head Head
Fork
A B C D A B C E
Origin Origin
Local repo Local repo
Head Head
A B C D A B C E
Outline
• Git overview
• Git vs. GitHub
• Basic Git commands
• Conflicts and merges
• Branches
• Recovering from errors
Getting Started with Clone
• git clone: Create a new local repository by copying a remote repo
$ git clone https://github.coecis.cornell.edu/cs4411-2020sp/ejt64-egos.git
A B C D
Local repo Partner’s repo
Head pull Head
push
A B C D A B C D
Understanding Git Status
~/egos$ git status Current branch
On branch master
Your branch is up to date with 'origin/master' Whether you have
unpushed commits
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
Changes you have
modified: src/grass/process.c added with git add
src/apps/tags
src/grass/tags These files still haven’t
been added to any commit
Ignoring Files You’ll Never Add
• Some files you never want ~/egos$ cat .gitignore
to commit: ctags files, # ctags files
compiled output, LaTeX aux tags
files… # LaTeX junk
*.aux
• Git will keep bothering you *.log
about them in git status *.bbl
• Add a file named # The debug log directory
logs/
.gitignore to the root # Object files in the build dir
of your repo, and then add build/**/*.o
it to a commit ~/egos$
https://www.atlassian.com/git/tutorials/saving-changes/gitignore
Diff: What Am I Committing?
~/egos$ git diff First file with
diff --git a/src/lib/queue.c b/src/lib/queue.c changes
index c638853..19106c3 100644
Line number
--- a/src/lib/queue.c
skipped to
+++ b/src/lib/queue.c
@@ -19,7 +19,7 @@ struct element {
void queue_init(struct queue *q){ Deleted from original
q->first = 0; (last commit)
q->last = &q->first;
- q->nelts = 0; Added in current
+ q->num_elements = 0; state of file
}
/* Put it on the wrong side of the queue. I.e., make it the next
@@ -34,7 +34,7 @@ void queue_insert(struct queue *q, void *item){
}
e->next = q->first; Skip ahead again
q->first = e; to line 34
Diff Details
~/egos$ git diff src/grass/process.c
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
deleted: src/apps/myprogram.c
Renaming
• Telling git you want to rename myprogram.c:
~/egos$ git mv src/apps/myprogram.c src/apps/newname.c
~/egos$ git status
On branch master
Your branch is up to date with 'origin/master'
Changes to be committed:
(use "git reset HEAD <file>…" to unstage)
A B C D
push
My local repo Partner’s local repo
Head Head
A B C D A B C E
Possible Outcomes
• No conflicts, just merge
~/egos$ git pull
# Editor pops up
Merge made by the 'recursive' strategy
src/lib/queue.c | 14 branch
Merge +++++++-------
'master' of https://github.coecis.cornell.edu/etremel/egos.git
1 file changed, 7 insertions (+), 7 deletions (-)
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit
D Head
A B C E M
Possible Outcomes
• Conflicting changes to the same file(s)
~/egos$ git pull
Auto-merging src/lib/queue.c
CONFLICT (content): Merge conflict in src/lib/queue.c
Automatic merge failed; fix conflicts and then commit the result
~/egos$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
…
Unmerged paths:
(use "git add <file>..." to mark resolution)
master
Branch Basics
~/egos$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'
master
D
Origin
A B C J
master
Pushing and Pulling
• You want to switch to a new branch your partner created
~/egos$ git checkout thread-develop
error: pathspec 'thread-develop' did not match any file(s) known to git
D E F
A B C J K M
L
Outline
• Git overview
• Git vs. GitHub
• Basic Git commands
• Conflicts and merges
• Branches
• Recovering from errors
Some Useful Incantations
• Oops! I made a commit but then made one small change
# Do this BEFORE you push
$ git add changed-file.c
$ git commit --amend --no-edit
• Oops! I want to edit the message on the commit I just made
$ git commit --amend
# Edit the message in your editor
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit
The Unnecessary Merge
• If I had known to pull first, I wouldn’t have to merge!
• First, ensure the merge aborts without making a merge commit
• Either due to genuine conflict, or by deleting the merge commit
message and saving (empty commit message aborts the commit)
• Then:
$ git reset --hard Discard changes made by merge
$ git reset HEAD^ Undo your last commit, leaving files changed
$ git stash
$ git pull # no merge this time Stash your changes, then pull again
$ git stash pop
$ git add foo.c bar.c # etc Re-do your commit on the new head
$ git commit
Further Reading
• Atlassian Git Tutorials:
https://www.atlassian.com/git/tutorials
• Detailed documentation on every command:
https://git-scm.com/docs
• Happy Git with R’s “Useful Git Patterns:”
https://happygitwithr.com/workflows-intro.html
• Oh Shit, Git!?! (source of my error-recovery examples):
https://ohshitgit.com/