git
Block 1.2: Tracking Changes in git
add
the change to the staging area it is stagedcommit
all staged changesgit add
and git commit
Files are added to the staging area with
git add <path to file or directory>
All files in the staging area are commited with
git commit
git add
Files are added to the staging area with
git add <path to file or directory>
.
# ...we write something into example.txt
git status
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# example.txt
Add example.txt
to the staging area
git add
Changes are added to the staging area with
git add <path to file or directory>
.
git add .
to add all unstaged changes to the staging area
.
always refers to the current directory*
to represent any sort of filename
.txt
files via *.txt
git commit
All changes in the staging area are commited with
git commit
git status
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: example.txt
Let’s commit the new change
git commit
Forgot the -m "insightful commit message"
?
git
will open an editor for you to write the message ingit
commands
#
at the beginning is a comment and will be ignored by git
(usually some helpful extra info)git
to proceedThe default editor in git is (usually) vim
vim
is notoriously hard to get out ofESC
, then write :wq
and hit Enter
code
commandgit config --global core.editor "code"
git-exercise
you created earlierhello.txt
with the contents “Hello!”add
)commit
)Check what’s happening between steps with git status
.
Check what’s happening between steps with git status
.
hello.txt
to read “Hello there!”add
)commit
)git status
👀You can see the high-level changes and what is about to happen with
git status
# ...we change the contents of example.txt
git status
# On branch main
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git restore <file>..." to discard changes in working directory)
# modified: example.txt
# no changes added to commit (use "git add" and/or "git commit -a")
git diff
👁️👄👁️You can see the actual unstaged changes, line by line with
git diff
# ...we changed the contents of example.txt
git diff
# diff --git a/example.txt b/example.txt
# index 10ddd6d..980a0d5 100644
# --- a/example.txt
# +++ b/example.txt
# @@ -1 +1 @@
# -Hello!
# +Hello World!
Note
git diff
only allows you to see changes to files which have already been commited before i.e. changing the contents of an existing file.
git reset
git reset
, removing all staged filesgit reset <filename / directory>
git restore
You can undo any changes to files with
git restore
# ...we changed the contents of example.txt
git status
# On branch main
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git restore <file>..." to discard changes in working directory)
# modified: example.txt
# no changes added to commit (use "git add" and/or "git commit -a")
git restore example.txt
git status
# On branch new-branch
# nothing to commit, working tree clean
git restore
You can undo any changes to files with
git restore
Git commands, like many other CLI tools follow a certain structure:
I’m aware this was a lot to take in.
library.txt
in your git-exercise
directorygit init -h
and copy the output into your library.txt
git status
and git diff
library.txt
and restore itgit <command> -h
and copy the output into your library.txt
library.txt
and restore itgit <command> -h
and copy the output into your library.txt
library.txt
and restore itgit
with source code heregit
can handle more than just code! 💪library.docx
and copy over the contents from library.txt
git stash -h
and copy the output into both library.docx
and library.txt
git diff
to compare how the same change is recognized by git in the two documentsAny Questions?