| title | Git and GitHub |
|---|---|
| author | UND Association for Computing Machinery |
| institute | https://linktr.ee/UND_ACM |
| date | \today |
| theme | Warsaw |
| colortheme | spruce |
| fontsize | 12pt |
| aspectratio | 169 |
| header-includes | \usepackage{hyperref} \hypersetup{colorlinks=true,urlcolor=blue} |
Git is a version control system (VCS) created by Linus Torvalds in 2005.
A VCS is a kind of "meta-save" that allows for storing the history of a software project.
They are primarily used for organization and collaboration.
A software project with its git history and source code is referred to as a repository (repo).
.gitdirectory (stores history)- working directory (the plaintext source code)
Git is unique from previous version control systems because it's decentralized.
The server/client dynamic is far less rigid (local/remote).
\pagebreak
GitHub is one example of where git repositories live.
It was founded in 2008 by
- Tom Preston-Werner
- Chris Wanstrath
- P. J. Hyett
- Scott Chacon
GitHub was acquired by Microsoft in 2018.
GitHub is not the only git forge, but it is the most popular despite being proprietary.
- GitLab
- Gitea
- Codeberg
- Sourcehut
- None (wait until the end)
\pagebreak
Reverting commits
- Merging branches
- Merging with upstream/remote
Fork: Git(Hub) term for a copy (of the source code).
As in a "fork in the road"
- Soft Fork (pull requests)
- Hard Fork
\pagebreak
Install it via your package management system of choice.
It is in your package management system, whether it's apt, pacman, brew, winget, etc.
For up-to-date install instructions, refer to git-scm.com/downloads
First, run git help
From then on git status and git log will be your best friends.
NAME="Your Name"
EMAIL="[email protected]"
git config --global init.defaultBranch main
git config --global user.name "$NAME"
git config --global user.email "$EMAIL"git init my-project
cd my-project
ls -la
ls -la .git
git status
git logecho "# My Awesome Project" >> README.md
git status
git log
git add README.md # aka "staging" README.md
git status
git commit -m "Initial Commit"
git log
git statusecho "Description of my awesome project" >> README.md
cat README.md
git status
git diffgit add README.md
echo "It supports both foo and bar" >> README.md
cat README.md
git status# git restore --staged README.md # unstage
# git restore README.md # discard
# git add README.md # updategit restore --staged README.md # unstage
git status
git add README.md # stage
git status
git diff
git diff --staged
git commit -m "Add project description"
git loggit rm README.md
git status
git commit -m "Remove README"
git loggit log
git revert --no-commit HEAD
git status
git log
git revert --continue\pagebreak
git log
git log -p
git log -p -n 1
git remote -v # outputs nothingCreate a new repo on GitHub: github.com/new
Fill out the form and click "Create repository"
Click the "SSH" button
git remote add origin "[email protected]:${USERNAME}/my-project.git"
git branch -M main
git push -u origin main
git remote -v\pagebreak
cd ..
rm -rf my-project/
git clone "[email protected]:${USERNAME}/my-project.git"
cd my-project# Pull changes from remote
git fetch --all
git merge origin
# git pull # equiv. to `git fetch && git merge`
# Push changes to remote
echo "Hello" > hello.txt
git add hello.txt
git commit
git push\pagebreak
Git allows users to temporarily view a previous commit by using git checkout and the commit id.
Git also has a feature called branching, which is used to "split the timeline" so to speak.
Notice that after checking out a commit, Git informs us we can create a new branch at this commit.
We may wish to do this to build new features on a known-working previous commit, rather than on main.
git checkout "$COMMIT_ID" # detached HEAD state
git switch -
git checkout "$COMMIT_ID" # detached HEAD state
git switch -c old-branch
git switch main
git branch -d old-branch # delete old-branchgit checkout jeff-branch # error: pathspec...
git branch
git branch jeff-branch
git checkout jeff-branch # or `git switch jeff-branch`
git branch
# git checkout -b jeff-branch
# git switch -c jeff-branch
git branch matt-branch
git branch\pagebreak
We'll populate each branch and then merge them, just as we had done earlier with main and origin/main.
git switch jeff-branch
echo "Hello, Jeff!" > hello.txt
git status
git add hello.txt
git commitgit switch matt-branch
echo "Hello, Matt!" > hello.txt
git status
git add hello.txt
git commit# git switch matt-branch # currently on matt-branch
git merge jeff-branch\pagebreak
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed fix conflicts and then commit the result
grep -rl "======="
grep -r "=======" -C 5
cat hello.txt
# Resolve the merge conflict in your text editor of choice<<<<<<< HEAD
Hello, Matt!
=======
Hello, Jeff!
>>>>>>> jeff-branch
git status
git add hello.txt
git commit
git switch main
git merge matt-branchgit push -u origin --all\pagebreak
EMAIL="[email protected]" # Should match your GitHub account
ssh-keygen -t ed25519 -C "$EMAIL"
eval "$(ssh-agent -s)" # Start the SSH agent
ssh-add "$HOME/.ssh/id_ed25519"
clear
cat "$HOME/.ssh/id_ed25519.pub" # Copy this outputGitHub > Settings > SSH and GPG keys > New SSH key
Title: <Describe the computer>
Key: <The contents of the .pub file>
ssh -T [email protected]
# Hi $USER! You've successfully authenticated, but GitHub
# does not provide shell access.See also Connecting to GitHub with SSH - GitHub Documentation
\pagebreak
Check "Keep my email address private"
Check "Block command line pushes that expose my email"
EMAIL="[email protected]"
git config --global user.email "$EMAIL"- Commit Messages: Imperative Mood
- "Add" instead of "Adding"
- "If applied, this commit will $COMMIT_MESSAGE"
- How to Write a Commit Message - cbeams
- Conventional Commits
- Git email flow vs GitHub flow
- Forking and Pull/Merge Requests (GitHub Flow)
- Submitting patches to Mailing Lists
.gitignorerebase,reflog,bisect- git-scm.com/docs
\pagebreak
# Renamed repo
git clone https://github.com/acm-ndsu/GitHub-Workshop \
GitHub-Workshop-but-cooler
# Bare repo
git clone --bare https://github.com/torvalds/linux linux.git
# Shallow Clones
git clone https://github.com/git/git --depth 2
# Local Clone
git clone ./my-project ./my-project-clone
cd ./my-project-clone
git remote -v\pagebreak
- Git is a version control system
- GitHub is a place where git repos live
- Git can be used independently of GitHub
git status
git log
git diff
git init
git clone
git add
git commitgit restore
git revert
git rm
git pull
git push
git merge
git branch
git checkout
git switchgit clone "$URL" # or `git init "$REPO_NAME"`
cd "$REPO_NAME"
git pull
git add "$FILE"
git status
git commit
git push
git log\pagebreak