6 Use git with a real project¶
6.1 Introducing the Explore California Website¶
We’ll use this project to initialize a git repository and make commits similar to a real world experience
6.2 Initialize git¶
ls -la
. There’s no.git
directory.git status
recognized git in a parent directory. But we can initialize a repository here.git init
.git log
returns error. HEAD is not pointed anywhere when there are no commits.git add .
to stage.git commit -m "Initial Commit"
.git log
now shows the SHA of the commit.git status
nothing to commit.
6.3 View file edits¶
Edit the 24 hour support number from 4315 to 4314
do this using your editor (replace everywhere in the directory)
git diff
to view changes. Shows changes with context before and after (typically 3 lines).@@ -129, 7 +129, 7 @@: Viewing line 129, and showing 7 lines.
-S
Enter to toggle wrapping and unwrappinggit diff
shows entire line with changeUse
git diff --color-words
to only view word changes.
6.4 Stage and commit shortcut¶
normal process is to stage, and then commit.
But if you are sure that you want to commit everything to the repository, then you skip the staging by
git commit -a file.txt
.Same as
git commit --all
. It stages and commits all changes to tracked files.Does not include untracked files.
Here we use
git commit -am "edits support number throughout website"
. Notice the optionsa
andm
can be combined.
6.5 View a commit¶
How do we view/review a previous commit?
change “you’ll” to “you all” in index.html
git show (insert SHA value)
(copy fromgit log
). This shows the git diff for that commitgit show (SHA) --color-words
works here as well.
6.6 Compare commits¶
But it’s more useful to compare commits.
git show SHA
shows diff of that commit with previousWhat if we want to compare it with an older commit?
git diff commit1..commit2
compares commits between the two.commit2 can be renamed simply as HEAD (for example).
6.7 Multiline commit messages¶
git commit -a
takes you to your editor (if setup properly)Convention: first line is main line (similar to a title)
then use a blank line followed by details.
Why blank line?
git log --oneline
is used for vieweing condensed log.
6.8 Make atomic commits¶
atomic commits are small commits
only affect a single aspect (smallest possibly and related to one thing)
easier to understand, to work with, and to find bugs.
improves collaboration
Workflow: make all changes
Add changes that are related to one thing to staging area, commit those.
Then add more, and commit.
6.9 Challenge: client edits¶
client email:
Change price Big Sur Retreat 620 to 750 (index.html and other pages)
California Calm from 250 to 270
Change title of Explorers page to “Make new friends” instead of “Come make a few friends”