0% found this document useful (0 votes)
17 views83 pages

2022 DH Toolbox Intro To Git and GitHub

pffffffffffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views83 pages

2022 DH Toolbox Intro To Git and GitHub

pffffffffffff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 83

Intro to Git and

GitHub
Yoo Young Lee
2022.09.28
Fall 2022 DH Toolbox Series
Learning Outcomes
• You will be able to understand basic concepts of version
control system and Git.
• You will be able to perform basic Git commands.
• You will be able to work with remotes.
• You will be able to use GitHub Desktop.
Schedule
Time Topic

11:30 – 11:50 Introduction to version control and Git

11:50 – 12:10 Using Git locally

12:10 – 12:30 Introduction to GitHub – Working with remotes

12:30 – 12:50 Collaboration

12:50 – 13:00 Wrap-up


Pre-workshop setup
• Install Git to your computer
Windows Mac
• git for windows • Download for macOS
• GitHub Desktop • GitHub Desktop
• Install any text editor that you like
• Atom
• Notepad++
• Sublime Text
• More
• For this workshop, simple Notepad works as well ☺
• Create a free GitHub account
Version Control Systems (VCS)
• Tracking and managing changes easily with one single
document

Image credit: https://smac-group.github.io/ds/github.html


Version Control System (VCS)
Version Control Systems (VCS)
• VCS lets you know when a file changed, who changed it, and
also easily roll back those changes.
• Best for text files, not ideal yet for binary files. VCS can store
images and videos, but not developed to easily track changes
in those files.
• Subversion
• Perforce Helix Core
Version Control System (VCS)
• Git
• CVS (Concurrent Versions System)
• Subversion (SVN)
• Mercurial
• Monotone
• Perforce Helix Core
• More
Git
• Free and open-source system and most powerful VCS.
• Distributed system: each contributor has a full copy of
repository which means that they can interact with the tracked
files without needing a coordinating server.
• Can be used on a client machine (e.g., your computer) or
server.
• Use Git with or without network.
• Beneficial for solo projects or multiple collaborators.
For today’s demo
• Using Mac, but you can follow me with Windows.
• Using iTerm2 terminal, but you can follow me with Git Bash
on Windows.
git config
• Set git configuration values on a global or local project level
• Global: apply to all repositories
• Local: apply to a specific repository
• Exercise 1: Common configuration settings: email and
username
• git config --global user.email "[email protected]"
• git config --global user.name "your_name"
• git config user.email
• git config user.name
• git config –list || git config –l
git init
• git init: Create a new Git repository and usually the first
command
• Exercise 2: We will create an empty folder (or directory) and
initialize Git using git init
• mkdir folder_name
• This command creates a new folder.
• cd folder_name
• This command moves to the new folder that we created.
• git init
• This command creates a new Git repository.
• ls -a
• This command lists the content and you can see .git
git init
Git

Image credit: https://www.javatpoint.com/git-index


git status
• git status: displays the state of the working directory and
the staging area.
• Exercise 3: Let's create a new file in the directory. Open any
editor you like and type "Hello everyone, this is for
2022 DH Toolbox." and save as "hello.txt"
• git status
Git

Image credit: https://www.javatpoint.com/git-index


git add
• git add: adds a change in the working directory to the
staging area.
• Exercise 4: Let's add the file "hello.txt" to the staging area.
• git add hello.txt
• git status
Git

Image credit: https://www.javatpoint.com/git-index


git commit
• git commit: captures a snapshot of the project’s currently
staged changes.
• Exercise 5: Let's commit a snapshot of the project's currently
staged changes.
• git commit -m "Meaningful commit message"
• Good commit message: a short description of the change (up to 50
characters), followed by one or more paragraphs giving more details
of the change (if needed)
Recap 1: Using Git
• Mostly one time
• git config
• git init

• Mostly used
• git status – git add – (git status) – git commit
Recap 1 - Exercise
• Exercise 6: Let’s modify the file, “hello.txt” and create
another file called, “DH2022.txt.”
• In the hello.txt: Add “Nice to meet you.”
• Create a file called, “DH2022.txt” with a line of “This is intro to
Git and GitHub at the 2022 Fall DH Toolbox Series.”
Recap 1 - Exercise
• Exercise 7: Let’s check git status.
• git status
Recap 1 - Exercise
• Exercise 8: Let’s add the modified file and new file to the
staging area.
• git add hello.txt
• git add DH2022.txt
• git status
Recap 1 - Exercise
• Exercise 9: Let’s commit them to the git repository with a
message.
• git commit –m “Added a new sentence in the hello.txt file
and created a new file, DH2022.txt”
git log
• git log: shows committed snapshots. It is a tool used for
examining a repository’s history and finding a particular
version of a project.
• Exercise 10: Let’s see a commit history in the repository.
• git log
git log
• git log: shows committed snapshots. It is a tool used for
examining a repository’s history and finding a particular
version of a project.
• Exercise 11: Let’s see what has changed.
• git log –p
• git log -2 || git log –n 2
• More git log cheatsheet
git log
Head and Master
• HEAD: the current commit your repository is on.
• Currently checked-out snapshot of your project.
• Master: the name of the default branch that git creates for you
when first creating a repository.
• In most cases, master means the main branch.
git show
• git show: used to view expanded details.
• Exercise 12: Let’s see what has changed in the specific
commit.
• git show commit_number
git diff
• git diff: shows changes between commits, commit and
working tree.
• Exercise 13: Let’s add a new sentence “Today is
Wednesday, September 28, 2022.” in the file, DH2022.txt.
• git diff
git diff
• git diff: shows changes between commits, commit and
working tree.
• Exercise 14: Let’s add it to staging and commit at the same
time as it is a small change.
• git status
• git commit –a –m “Added a date in the DH2022.txt file”
• git diff
Recap 2: Ger more information about changes
• git log
• git show
• git diff
git rm
• git rm: used to remove files from a Git repository. Tell git not
to track a file anymore.
• Exercise 15: Let’s not to track DH2022.txt file.
• git rm DH2022.txt
• git status
• git commit -m “Removed DH2022.txt from the git repository
as we don’t need it anymore”
git mv
• git mv: used to rename or move a file, a directory, or a
symlink
• Exercise 16: Let’s rename hello.txt to hi.txt.
• git mv hello.txt hi.txt
• git status
• git commit -m “Renamed hello.txt to hi.txt”
.gitignore
• .gitignore: indicates files which Git has been explicitly told
to ignore
• dependency caches
• compiled code
• files generated at runtime
• hidden system files
• Etc.
.gitignore
• Exercise 17: Let’s create .gititnore file and create a directory called
photos. Open .gititnore file in any text editor. Then, add
photos/ to tell Git to ignore anything in photos directory.
• touch .gitignore
• mkdir photos
• ls –la
• git status
• git add .gitignore
• git commit -m
“Added .gitignore
file.”
.gitignore
• Exercise 18: Let’s add a photo to photos/ and see how Git
behaves.
• git status
Recap 2: Delete and rename files
• git rm
• git mv
• .gititnore
git checkout
• git checkout: act of switching between different versions –
files, commits, and branches. Used to undo changes in the
working directory.
• Exercise 19: Let’s add more sentences in the hi.txt file. But
you realized that this is not what you wanted.
• git status
• git checkout hi.txt
• git status
git restore
• git restore: restores working tree files and helps to unstage
or even discard uncommitted local changes.
• Exercise 20: Let’s add more sentences in the hi.txt file and
add it to the staging area. Then, you realized that this is not
what you wanted.
• git status
• git add hi.txt
• git status
• git restore --staged hi.txt
• git status
• git restore hi.txt
• If you want to discard changes in working directory – similar to git checkout
file_name
git commit --amend
• git commit --amend: overwrites the previous commit and
allows you to modify and add changes to the most recent
commit. Avoid to use it if commits are already made public.
• Exercise 21: Let’s create a new file fall.txt file and add it to
the staging area and then commit.
• touch fall.txt
• git status
• git add fall.txt
• git commit -m “Added a new file called fall.txt”
git commit --amend
• git commit --amend: overwrites the previous commit and
allows you to modify and add changes to the most recent
commit. Avoid to use it if commits are already made public.
• Exercise 21: (Continued) Then you realized that you didn’t
add any content in the file. So add few sentences.
• git add fall.txt
• git commit --amend –m “Added a new file called fall.txt
with content”
git commit --amend
git commit --amend
git revert
• git revert: creates a new commit with inverse changes and
cancels previous changes instead of making it as though the
original commit never happened.
• Exercise 22: Let’s add more sentences to fall.txt. Then,
you don’t like the sentence that you just added it.
• git status
• git commit -a -m “Commit_message”
• git revert HEAD
• git log -2
git revert
Recap 3: Undo changes
• git checkout
• git restore
• git commit --ammend
• git revert
Branch

Image credit: https://medium.com/@aakarr.me/how-to-create-branch-in-git-6118de02a7c6

• Branch: A pointer to a particular commit


• Master: Default branch that git creates for you when a new
repository is initialized and normally known as clean working
state
git branch
• git branch: used to list, create, delete and manipulate
branches
• Exercise 23: Let’s play with git branch.
• git branch: list branches
• git branch name_of_branch: create a branch
git checkout
• git checkout: used to checkout the latest snapshot for both
files and for branches
• Exercise 24: Let’s play with git checkout to switch to a
branch.
• git checkout name_of_branch: switch to the name_of_branch
• git branch
git checkout
• git checkout -b: used to create a new branch and switch to
it
• Exercise 25: Let’s play with git checkout -b to create and
switch to a branch at the same time.
• git checkout -b name_of_branch
• git branch
Working with branches
• Exercise 26: Let’s create a new file, dynamite.txt in the
lyrics-dynamite-bts branch
• touch dynamite.txt
• git status
• git add dynamite.txt
• git status
• git commit -m “Added a new file, dynamite.txt”
Working with branches
Working with branches
• Exercise 26: (continued) Let’s create a new file, dynamite.txt
in the lyrics-dynamite-bts branch
• git log -2
git checkout
• Exercise 27: Let’s switch back to master branch.
• git checkout master
• git branch
git branch
• Exercise 28: Let’s delete lyrics_dynamite branch.
• git branch -d lyrics_dynamite
• git branch
Master branch
lyrics-dynamite-bts branch
Branch

Image credit: https://medium.com/@aakarr.me/how-to-create-branch-in-git-6118de02a7c6

• Branch: A pointer to a particular commit


• Master: Default branch that git creates for you when a new
repository is initialized and normally known as clean working
state
git merge
• git merge: used to merge a branch to master
• Exercise 29: Let’s merge changes made in the lyrics-
dynamite-bts branch to the master branch
• git status: make sure you’re on the master branch
• git merge lyrics-dynamite-bts
• ls –l
• git log --graph --oneline
git merge
Recap 4: Branching and merging
• git branch
• git merge
Working with remotes

Image credit: https://pratik26.medium.com/git-github-in-8-hours-fdbeaba4635e


Online remote hosting services
• GitHub
• GitLab
• BitButcket
GitHub
• Internet hosting service for software development and version
control using Git.
• Features provided such as distributed version control system
of Git, access control, bug tracking, software feature requests,
task management, continuous integration, wiki, etc.
• Free for educational purpose and open-source software
• https://github.com/topics/dh
• https://github.com/topics/digital-humanities
git remote
• git remote: manages set of tracked repositories
• Exercise 30: Let’s create a repository in GitHub that will be
synced with your local repository.
• Create a new repository first in GitHub.
• Choose private for now.
• To avoid errors, do not initialize the new repository with README,
license, or gitignore files.
git remote
git remote
• Exercise 30: (continued) Let’s create a repository in GitHub
that will be synced with your local repository.
• git remote add origin remote_url
• git remote -v
git push
• git push: uploads local repository content to a remote
repository.
• Exercise 31: Let’s put your local repository to GitHub.
• Generate your personal access token
• Settings
• Developer settings
• Personal access tokens
• Git Credential Manager
• git push origin master || git push –u origin master
• username: your_username
• password: your_personal_access_token
git fetch
• git fetch: downloads content from a remote repository into
your local repository
• Exercise 32: Let’s add README.md in GitHub.
• git remote show origin
• git fetch origin master
• git log origin/master -2
• git status
• git merge origin/master or git pull: To update the content
git fetch
git pull
• git pull: fetches and downloads content from a remote
repository and immediately update the local repository to
match that content
• Exercise 33: Let’s update README.md in GitHub.
• git pull origin master || git pull
GitHub
Recap 5: Add your local repository to GitHub
• git remote
• git push
• git fetch
• git pull
GitHub Desktop
• An application that enables you to interact with GitHub using
a GUI (Graphical User Interface) instead of the command line
or a web browser.
• Login with your free GitHub account
git fork
• git fork: creates a copy of the original repository (upstream
repository).
• Exercise 34: Let’s fork InfiniteUlysses.com repo into your
GitHub account.
git fork
git clone
• git clone: used to point to an existing repository and copy of
that repo in a new directory, at another location.
• Exercise 35: Let’s clone InfiniteUlysses.com repo that you just
forked into your local machine via GitHub Desktop.
git clone
Recap 6: Let’s prepare to work together
• Exercise 36: let’s fork and clone yooylee/DH-2022-Test-Run
repo via GitHub Desktop.
Pull request
• Exercise 37: let’s add more lyrics to dynamite.txt file by
creating a new branch via GitHub Desktop.
• New branch
• Commit
• Pull request
Collaboration Tips
• Always synchronize your branches before starting any work on
your own.
• Avoid having large changes that modify a lot of different
things.
• Have the lates version of the project in the master branch.
• Have good commit messages.
• Describe why merge is needed when you create a pull
request.
More Resources
• GitHub Docs
• Learn Git by Bitbucket

You might also like