Git in 4 Weeks
Git in 4 Weeks
Version 1.0
05/25/21
• If you don’t already have one, sign up for free GitHub account
at
http://www.github.com
Brent Laster
Tech Skills Transformations
Professional
Git
Book
• Extensive Git reference,
explanations,
• and examples
• First part for non-technical
• Beginner and advanced
reference
• Hands-on labs
Jenkins 2
Book
• Jenkins 2 – Up and Running
• “It’s an ideal book for those
who are new to CI/CD, as well
as those who have been using
Jenkins for many years. This
book will help you discover
and rediscover Jenkins.” By
Kohsuke Kawaguchi, Creator
of Jenkins
• Speed
• Simple design
• Strong support for branching
• Distributed nature
• Ability to handle large projects efficiently
• Disconnected support
Debian/Ubuntu
$ apt-get install git
Fedora (up to 21)
$ yum install git
Fedora (22 and beyond)
$ dnf install git
FreeBSD
$ cd /usr/ports/devel/git
$ make install
Gentoo
$ emerge --ask --verbose dev-vcs/git
OpenBSD
$ pkg_add git
Solaris 11 Express
$ pkg install developer/versioning/git
Clone
Commit
C
h C
e o
c m
k m
o i
u t
t
F
Push Clone e
t
c P
Prod Local Repository h u
l
C l
h Commit
e
c
Staging Area
Test k
o
u
Add
t
Local Machine
CVS
dir: proj1
file1.java
Snapshot
Commit
file2.java
Commit Git
Prepare
• Stores info about what will go into next commit
• Allows you to control what part of the working tree go
into the next commit
• Allows you to queue up individual changes
• Bundle a collection of specific changes to go into a
commit
• Get them out of your working directory (i.e. “check
them off”) Local Repository
• Helps you split up one large change into multiple commits
Commit
Repair
• Allows you to “amend” last commit – redo
Staging Area
Merging with conflicts
• When merge happens, changes that merge cleanly are Add
updated both in working directory and in staging area
• Changes that did not merge cleanly are left in your
working directory Working Directory
• Diff between working directory and staging area shows
differences
Commit A Commit B
Local Repository
Staging Area
Change 1
File 1 File 2 File 3
Change 2
Working Directory
Commit A
*
Local Repository
File 1 File 3
*
Staging Area
Change 1
File 1 File 2 File 3
Working Directory
• Notes
§ Use --global to persist options for all repos of this user
§ Check config value with
§ $ git config --list (or git config –l)
§ $ git config <property> as in git config user.name
§ Can also set many other things – like diff tool and end-of-line
settings
© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
Configuring your default editor in Git 32
• Editor defaults (generally)
§ Windows : notepad, Linux : vim
• Can set via OS (for example export EDITOR variable)
• Setting for Git only
§ Set core.editor in your config file: git config --global core.editor “vim”
§ Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim
• Examples
§ git config core.editor vim (Linux)
§ git config --global core.editor “nano” (mac)
§ git config --global core.editor "'c:\program files\windows
nt\accessories\wordpad.exe'"(windows)
§ git config --global core.editor "'C:/Program Files
(x86)/Notepad++/notepad++.exe'"
§ (Git bash shell for Windows)
© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
Labs Logistics 33
Work through the steps – some questions for thought included
Creating Files
In the workshop, we’re not concerned about contents of files.
We just need some files to play with.
Purpose: In this lab, we’ll create an empty Git repository on your local disk and stage and commit
content into it.
$ tree.sh -* .git
Git snapshot COMMIT_EDITMSG
HEAD
config
description
hooks
applypatch-msg.sample
commit-msg.sample
post-commit.sample
post-receive.sample
Working directory post-update.sample
pre-applypatch.sample
pre-commit.sample
dir: proj1 pre-rebase.sample
prepare-commit-msg.sample
update.sample
index
info
file1.java exclude
logs
HEAD
refs
heads
file2.java objects
master
3a
0e351dc84e32abec5d4dd223c5abdabd57b7f5
4c
7637a4b66aefc1ee877eaa1afc70610f0ee7cc
80
7805ea7ae9fdf1b06e876af6e9a69a349b52a3
88
bee8f0c181784d605eabe35fb04a5a443ae6b7
ba
2906d0666cf726c7eaadd2cd3db615dedfdf3a
info
*
pack
*
refs
heads
master
tags
*
Remote Repository
The repository (.git directory) lives in Remote Repository
the local directory tree.
Directory
Directory
Local
Repository Local Directory
Repository Local
Repository
Directory
Directory
Local
Repository Local
Repository
System
• git <command> -h
Brings up on screen list of options
core]
repository= 0
filemode = false
System
bare = false
logallref= true
core]
repository= 0
filemode = false
User 1
bare = false
logallref= true
Directory
Directory
Local [core]
Directory
Repository
repository= 0
filemode = false
bare = false
Local [core]
logallref= true repository= 0
Repository repository= 0
filemode = false
bare = false
logallref= true
Directory
Directory
Local
Local
[core]
Repository
repository= 0
filemode = false [core]
bare = false repository= 0 core]
Repository
logallref= true filemode = false repository= 0
bare = false filemode = false
User 2
logallref= true bare = false
logallref= true
» ?? = untracked
» M = modified
» A = added
» D = deleted
» R = renamed
» C = copied
» U = updated but unmerged
Staging
b Area
Add
Working
ab Directory
Local Machine
Purpose: In this lab, we’ll work through some simple examples of updating files in a Local
Environment and viewing the status and differences between the various levels along the way.
54
© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
Notes about Tags 55
Purpose: In this lab, we’ll work through some simple examples of using the Git log commands to
see the flexibility it offers as well as creating an alias to help simplify using it. We’ll also look at
how to tag commits to have another way to reference them.
• A git attributes file is a simple text file that gives attributes to pathname –
meaning git applies some special setting to a path or file type.
• Attributes are set/stored either in a .gitattributes file in one of your
directories (normally the root of your project) or in the .git/info/attributes file
if you don’t want the attributes file committed with your project.
• Example use: dealing with binary files
• To tell git to treat all obj files as binary data, add the following line to your
.gitattributes file:
• *.obj binary
§ With this setup, git won’t try to convert or fix eol issues. It also won’t try
to compute or print a diff for changes in this file when you run git show or
git diff on your project.
• Advantage is that this can be put with your project in git. Then, the end of
line configuration now travels with your repository. You don't need to worry
about whether or not all users have the proper line ending configuration.
• Some sample gitattributes files in GitHub for certain set of languages.
• Command: git rm
• What it does:
§ Removes it from your working directory (via rm <file>) so it
doesn’t show up in tracked files
§ Stages removal
• Then you do the commit to complete the operation
• If you have a staged version AND a different modified version, git will
warn you
§ Use the –f option to force the removal
• Can remove just from the staging area using --cached option on git
rm
• Can provide files, directories, or file globs to command