0% found this document useful (0 votes)
76 views

Git in 4 Weeks

The document provides an overview of prerequisites and materials for a 4-week Git training workshop. It discusses installing Git, the Git ecosystem including public and private hosting options, and the centralized vs distributed version control models. The instructor Brent Laster is introduced, including his background and authored books on Git and Jenkins.

Uploaded by

afreeman.recycle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Git in 4 Weeks

The document provides an overview of prerequisites and materials for a 4-week Git training workshop. It discusses installing Git, the Git ecosystem including public and private hosting options, and the centralized vs distributed version control models. The instructor Brent Laster is introduced, including his background and authored books on Git and Jenkins.

Uploaded by

afreeman.recycle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

1

Version 1.0
05/25/21

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Prerequisites 2

• Have a recent version of Git downloaded and running on your


system
§ For Windows, suggest using Git Bash Shell interface

• If you don’t already have one, sign up for free GitHub account
at
http://www.github.com

• Workshop doc is in https://github.com/skilldocs/git4

• Labs doc for workshop


https://github.com/skilldocs/git4/blob/main/git4-1-labs.pdf

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
3

Git In Four Weeks


Week 1

Brent Laster
Tech Skills Transformations

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
4
• Founder, Tech Skills Transformations LLC
• R&D DevOps Director

About me • Global trainer – training (Git, Jenkins,


Gradle, CI/CD, pipelines, Kubernetes, Helm,
ArgoCD, operators)
• Author -
• OpenSource.com
• Professional Git book
• Jenkins 2 – Up and Running book
• Continuous Integration vs. Continuous
Delivery vs. Continuous Deployment
mini-book on Safari
• https://www.linkedin.com/in/brentlaster
• @BrentCLaster
• GitHub: brentlaster

© 2021 Brent C. Laster &


© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
5

Professional
Git
Book
• Extensive Git reference,
explanations,
• and examples
• First part for non-technical
• Beginner and advanced
reference
• Hands-on labs

© 2021 Brent C. Laster &


© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
6

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

© 2021 Brent C. Laster &


© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
O’Reilly Training 7

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
What is Git? 8

• Distributed Version Control System


• Open source
• Available for multiple platforms
• Roots in source control efforts for Linux Kernel
• Primary developer – Linus Torvalds
• Been around since 2005

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Design Goals 9

• Speed
• Simple design
• Strong support for branching
• Distributed nature
• Ability to handle large projects efficiently
• Disconnected support

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Installation 10

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

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Ecosystem: Public hosting 11

• Github, Bitbucket, etc.

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Ecosystem: Self-hosting 12

• GitLab, Enterprise GitHub

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Ecosystem: Ease-of-use Packages 13

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Ecosystem: Tools that Incorporate Git 14

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Interfaces 15
• Many different interfaces exist for working with Git
• Many GUI interfaces
• Command line interface
• Products that script the command line
• Other products such as Gerrit that wrap around Git
• We teach the command line interface because
§ GUI interfaces can all be different - there is not a standard
§ GUI interfaces may change or go away
§ If a GUI interface does not provide some piece of Git
functionality, the functionality can be done with the command
line
§ If you understand the command line, you can usually find which
command in a GUI is used to do the operation

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Centralized vs. Distributed VCS 16

Centralized Version Control Model Distributed Version Control Model

Central Local Remote


Server Environment Repository
Local
Machine
Check Out

Clone

Commit
C
h C
e o
c m
k m
o i
u t
t

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
17
Git in One Picture
Server

Public Remote Repository

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

Dev Working Directory

Local Machine

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Granularity (What is a unit?) 18

• In traditional source control, the unit of granularity is


usually a file file1.java
Delta

CVS

• In Git, the unit of granularity is usually a tree


Working directory

dir: proj1

file1.java
Snapshot

Commit

file2.java

Commit Git

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Delta Storage Model 19

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Snapshot Storage Model 20

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Compressing Files 21

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Staging Area: Why? 22

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

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Example Workflow and Terms 23

1. Modify files in working directory


2. Stage files, adding snapshot to staging area
3. Commit – promotes files from the staging area into permanent
snapshot in the local repo (stored in .git directory)

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Initializing a Repo and Adding Files 24
(commands)
• git init
§ For creating a repository in a directory with existing files
§ Creates repository skeleton in .git directory
• git add
§ Tells git to start tracking files
§ Patterns: git add *.c or git add . (. = files and dirs recursively)
• git commit
§ Promotes files into the local repository
§ Uses –m to supply a comment
§ Commits everything unless told otherwise

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
SHA1 25
$ git commit -m "initial add of file1.c"
[master b41b186] initial add of file1.c
1 file changed, 3 insertions(+)
create mode 100644 file1.c

• Everything in Git is check-summed before it is stored and is then referred to by that


checksum. Built into Git low-level functions.
• That means it’s not possible to change the contents of any file or directory without Git
knowing about it. i.e. avoids file corruption or tampering.
• Mechanism that Git uses for this checksum is called a SHA-1 hash.
• 40-character string of hex characters.
• Calculated based on contents of a file or directory struture.
• Example: b41b186552252987aa493b52f8696cd6d3b00373
• Used throughout Git.
• Git stores/points to everything by hash value of the contents in its database.
Remember: You can always get a handle to a snapshot via a SHA1.

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Committing Changes 26

• Commits to local repo only - not remote


• Records a snapshot of your local working area that you can switch
back to or compare to later
• Requires commit message
§ Pass by –m “<message>”
§ If no –m, brings up configured editor so you can type it
• Commit output
- which branch you committed to
- what SHA-1 checksum the commit has
- how many files were changed
- statistics about lines added and removed in the commit.

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Workflow 27

Commit A Commit B

Local Repository

Staging Area

Change 1
File 1 File 2 File 3
Change 2
Working Directory

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Undoing/Updating Things 28

• git commit --amend


§ Allows you to “fix” last commit
§ Updates last commit using staging area
§ With nothing new in staging area, just updates comment
(commit message)
§ Example:
» git commit –m “my first update”
» git add newfile.txt (add command stages it into staging
area)
» git commit --amend
» This updated commit takes the place of the first (updates
instead of adding another in the chain)

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Workflow 29

Commit A
*
Local Repository

File 1 File 3
*
Staging Area

Change 1
File 1 File 2 File 3

Working Directory

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Updating a file that Git knows about 30
• How do we update a file that git knows about?
§ Stage it (git add .)
§ Commit it (git commit)
§ Shortcut syntax : git commit –am “<comment>”
» Doesn’t add new files (untracked ones)

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Initial User Configuration 31

• Command: git config


• Main things to set
§ User name
$ git config --global user.name "John Doe"
§ Email address
$ git config --global user.email [email protected]

• 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.

Lazy way to create a file – in a terminal:


$ echo stuff > file1.c (even lazier way $ date > file1.c)

Lazy way to append to a file – in a terminal:


$ echo “more stuff” >> file1.c

Can use editor if you prefer…

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
34

Lab 1 - Creating and Exploring a Git Repository and Managing Content

Purpose: In this lab, we’ll create an empty Git repository on your local disk and stage and commit
content into it.

© 2021 Brent C. Laster &


@techupskills techupskills.com |
| techskillstransformations.com
Git and File/Directory Layouts 35
Local Repository File Layout

$ 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
*

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
36
Starting Work with Git – Multiple Repositories

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

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Autocomplete 37

• Enabled in BASH shell


• Can be enabled in other environments via file in git source
• Git <start of command><tab> (may have to hit it twice if multiple
options)
• Git command --<start of option><tab>
• Examples:
§ git co <tab><tab>
§ git com <tab>
§ git list --s<tab> (note double –’s)

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Getting Help 38

• git <command> -h
Brings up on screen list of options

• git <command> --help


• git help <command>
Brings up html man page

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Line Endings 39

$ git config core.autocrlf

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Configuration Files 40

System Configuration File (if exists)


System: <usr|usr/local> /etc/gitconfig Scope: All users on system
[core]
symlinks = false git config --system
autocrlf =Global:
input ~/.gitconfig
[color]
[user]
diff = auto Global Configuration File
statusname
= auto= Brent Laster Scope: Current User
branch email
= auto=
Local: <repo dir>/.git/config
[email protected]
interactive = true git config --global
[pack][diff] [core]repositoryformatversion = 0
mnemonicprefix
packSizeLimit = true
= 2g = false
filemode
[help] [auto] bare = false Local Configuration File
formatcrlf = false
= htmllogallrefupdates = true Scope: Local repository
[push] symlinks = false
defaultignorecase
= matching
= true
git config or git config --local
[color] hideDotFiles = dotGitOnly
[remote "origin"]
ui = autofetch = +refs/heads/*:refs/remotes/origin/*
url = git@cvsadmin:class/sas/subs
[branch "master"]
remote = origin
merge = refs/heads/master

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
41
Git Configuration Layout – Multiple Configs
Remote Repository
Remote Repository

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 filemode = false


bare = false
logallref= true
Local [core]

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

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
How Git reports status on files 42
Files in your working directory can be in one of two main states:
• Tracked
• Were in the last snapshot (last commit)
• Can be
• Unmodified - same as what’s in Git
• Modified - different from what’s in Git
• Staged
- OR -
• Untracked
• Everything else
• Not in last snapshot
• Not in staging area

3 questions to think about for determining status

• Is Git aware of the file (is it in Git)? tracked or untracked


• What is in the staging area?
• What is the relationship of the latest version in Git to the version in the working
directory?

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
43
1.
File Status Lifecycle
Files start out in wd (rev a)
Git aware? No – untracked files
Anything staged? No = Changes not staged for commit
Git compared to WD: N/A
Local
2. (Git) Add file. (5)b Repository
Git aware: Yes – tracked
Anything staged? Rev a staged = Changes to
Commit
be committed
Git compared to WD: Same - unmodified
Staging
3. Edit in working directory (rev b) (4)b
(2)a
Area
Git aware: Yes – tracked
Anything staged? Rev a staged (to be committed)
Git compared to WD : Different – modified
Add
4. (Git) Add file. Tracked, rev b staged (rev a
Working
overwritten), unmodified ((3)b
1)a Directory

5. (Git) Commit. Tracked, Staged: Nothing, Local Machine


unmodified
“nothing to commit (working directory clean)” * “Changes to be committed” = staged
* “Changes not staged for commit” = wd

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Status 44

• Command: git status


§ Primary tool for showing which files are in which state
§ -s is short option - output <1 or more characters><filename>

» ?? = untracked
» M = modified
» A = added
» D = deleted
» R = renamed
» C = copied
» U = updated but unmerged

§ -b option – always show branch and tracking info


• Common usage: git status -sb

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
45
Git Special References: Head and Index
• Head
§ Snapshot of your last commit
§ Next parent (in chain of commits)
§ Pointer to current branch reference (reference = SHA1)
§ Think of HEAD as pointer to last commit on current branch
• Index (Staging Area)
§ Place where changes for the next commit get registered
§ Temporary staging area for what you’re working on
§ Proposed next commit
• Cache - old name for index
• Think of cache, index, and staging area as all the same

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Showing Differences 46

• Command : git diff


• Default is to show changes in the working directory that are not yet
staged.
• If something is staged, shows diff between working directory and
staging area.
• Option of --cached or --staged shows difference between staging
area and last commit (HEAD)
• git diff <reference> shows differences between working directory
and what <reference> points to – example “git diff HEAD”

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Understanding File Diffs 47
1.Assume
2.Let’s
3. 4.
We Now,
canadd wefile
also (rev a)
modify
compare created,
the file added,and
in the
explicitly WD –
our change to the Staging
5. If we
commited
against use
rev b.(index).in the
local
the Local Repo.–staged
repo. or
WD –cached
version
Area
option:
unchanged.
Git diff compares:
GitGit
diff
diffHEAD compares:
compares:
WD Git
Git
WD diff
diff
to to
Local--staged
compares:
toStaging
SARepo
(nothing compares: Local
WD Area there),
Staging
WD Area
toDifferent
SA to Local
(nothing Repo
there), a Repository
So compares
Results:
Results: Same to Local Repo
Results:
So comparesDifferent
to Local Repo
Results:
Output:
Output: Different
Differences
Nothing between WD and
Output:
Local RepoDifferences between Staging
Results:
Output: Differences
Same between WD and Commit
Area
Output: and
Local Repo Local
Nothing Repo

Staging
b Area

Add

Working
ab Directory

Local Machine

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
48

Lab 2 - Tracking Content through the File Status Lifecycle

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.

© 2021 Brent C. Laster &


@techupskills techupskills.com |
| techskillstransformations.com
History 49
• Command: git log
• With no options, shows name, sha1, email, commit message in
reverse chronological order (newest first)
• -p option shows patch – or differences in each commit
§ Shortcut : git show
• -# -shows last # commits
• --stat – shows statistics on number of changes
• --pretty=oneline|short|full|fuller
• --format – allows you to specify your own output format
• --oneline – show only one line for each entry
• Time-limiting options --since|until|after|before (i.e. --
since=2.weeks or --before=3.19.2011)
• gitk tool also has history visualizer

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Sample git log format 50

• git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short


• In detail…
§ --pretty="..." defines the output format.
§ %h is the abbreviated hash of the commit
§ %d commit decorations (e.g. branch heads or tags)
§ %ad is the commit date
§ %s is the comment
§ %an is the name of the author
§ --graph tells git to display the commit tree in the form of an
ASCII graph layout
§ --date=short keeps the date format short and nice

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git log examples 51

• git log --pretty=oneline --max-count=2


• git log --pretty=oneline --since='5 minutes ago'
• git log --pretty=oneline --until='5 minutes ago'
• git log --pretty=oneline --author=<your name>
• git log --pretty=oneline --all
• git log --oneline --decorate

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Aliases 52
• Allow you to assign alternatives for command and option strings
• Can set using
§ git config alias.<name> <operation>
• Example
§ git config --global alias.co checkout
§ After, “git co” = “git checkout”
• Example aliases (as they would appear inside config file)
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --
date=short

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Seeing History Visually 53
• gitk – graphical interface that comes with most git
• Shows history of local repository (not remote) in working directory
• Also useful to be able to see how changes look graphically

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Tags in Git 54

• Command: git tag


• Two types
§ Lightweight – like regular tag, pointer to specific commit
§ Annotated – stored as full object in the database
» Checksummed
» Contain full email and date, tagger name, and comment
» Created by git tag –a <tag> -m <message>
• Create a tag
§ Git tag <tagname> <hash>
• Show tags
§ Git tag (lists tags out)
§ Git show <tag> - shows detail

54
© 2021 Brent C. Laster &
@techupskills techupskills.com | techskillstransformations.com
Notes about Tags 55

• You tag commits (hashes), not files


• A tag is a reference (pointer) to a commit that stays with that
commit
• To reference a file via a tag, you need to qualify with the file name
(since the tag refers to entire commit)

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
56

Lab 3 - Working with Changes Over Time and Using Tags

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.

© 2021 Brent C. Laster &


@techupskills techupskills.com |
| techskillstransformations.com
Supporting Files - .gitignore 57

• Tells git to not track files or directories (normally listed in a file


named .gitignore)
• Operations such as git add . will skip files listed in .gitignore
• Rules
§ Blank lines or lines starting with # are ignored.
§ Standard glob patterns work.
§ You can end patterns with a forward slash (/) to specify a
directory.
§ You can negate a pattern by starting it with an exclamation point
(!).
• What types of things might we want git to ignore?

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Supporting Files - .gitattributes 58

• 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.

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Git Attributes File – Example 59
• Basic example

• 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.

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
Removing files 60

• 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

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com
61

That’s all - thanks!

© 2021 Brent C. Laster &


@techupskills techupskills.com | techskillstransformations.com

You might also like