gitBlock 1.2: Tracking Changes in git
Changes can be either unstaged, staged or commited.
add the change to the staging area it is stagedcommit all staged changesgit add and git commitFiles 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 addFiles 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.txtAdd example.txt to the staging area
git addChanges 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 *.txtgit commitAll 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.txtLet’s commit the new change
git commitForgot 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 Entercode 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 resetgit reset, removing all staged filesgit reset <filename / directory>git restoreYou 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 cleangit restoreYou 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.txtgit status and git difflibrary.txt and restore itThe labels in Sourcetree mirror the names within the git CLI
git <command> -h and copy the output into your library.txtlibrary.txt and restore itSince VSCode is primarily a text editor, git functionality is hidden in the sidebar
git <command> -h and copy the output into your library.txtlibrary.txt and restore itgit with source code heregit can handle more than just code! 💪library.docx and copy over the contents from library.txtgit stash -h and copy the output into both library.docx and library.txtgit diff to compare how the same change is recognized by git in the two documentsAny Questions?