Working ------ git add -----> Stage Index ------ git commit -----> Repository
- Git generates a checksum for every change set (commit)
- Example: 987e06a6d47b724223b40c2ccda229c58fa0b63f
HEAD points to the latest commit in our repository (not stage and not working)
cat .git/HEAD
cat .git/refs/heads/master
git init
Add files from working tree to stage index
git add file.txt
This will show the diff between repository vs working tree
git diff file.txt
This will show the diff between repository vs stage index
git diff --staged
git rm file.txt
git mv file.txt file2.txt
git log
git log -n 5
git log --author="Gustavo"
You don't need to use de full author's name.
git log --since=2018-05-05
git log --until=2018-05-05
git log --grep="Init"
Make the working directory
file index.html
look like the same version that is in my repository
git checkout -- index.html
Change the index.html
to the last commit (HEAD) of our current branch/repository (master)
git reset HEAD index.html
It is possible to change the last commit.
git add file_to_be_included_to_last_commit.html
git commit --amend -m "forgot to add this file to the last commit"
git log
We can go back a file to a specific commit. First we need to know which commit we would like to go back using git log.
git log
git checkout 987e06a6d47b724223b40c2ccda229c58fa0b63f -- index.html
Git revert create a new commit with the commit we choose from git log
git log
git revert 987e06a6d47b724223b40c2ccda229c58fa0b63f
git reset
allow to specifie where the HEAD pointer
should point to.
- --soft: does not change stagin index or working directory
- --mixed (default): changes staging index to match repository but does not change working directory
- --hard: changes staging index and working directory to match repository
git reset --soft 987e06a6d47b724223b40c2ccda229c58fa0b63f
git reset --mixed 987e06a6d47b724223b40c2ccda229c58fa0b63f
git reset --hard 987e06a6d47b724223b40c2ccda229c58fa0b63f
# Check what would happen
git clean -n
# Remove untracked file that are not in stage index
git clean -f
We need to create a file called .gitignore
inside our root repo.
git config --global core.excludesfile /etc/.gitignore_global
.gitignore_global use the same rules applied to .gitignore
Git will not ignore files that have already been tracked before the creation of .gitignore
# We can remove the file from repository and original
git rm index.html
# If we want to keep the file in our repository
# We remove the file only from staging index
git rm --cached index.html
Git ignores empty dirs
touch my_tracked_dir/.gitkeep
tree-ish
We can reference a commit by using:
- full SHA-1 hash
- short SHA-1 hash
- at least 4 chars
- from 8 to 10 chars
- HEAD pointer
- branch reference, tag reference
- ancestry
- Parent commit
- HEAD^
- acf87504^
- master^
- HEAD
1, HEAD
- Grandparent commit
- HEAD^^
- acf87504^^
- master^^
- HEAD~2
- Parent commit
git ls-tree HEAD
git ls-tree master
git ls-tree master index.html
git ls-tree master^ index.html