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/mastergit initAdd files from working tree to stage index
git add file.txtThis will show the diff between repository vs working tree
git diff file.txtThis will show the diff between repository vs stage index
git diff --stagedgit rm file.txtgit mv file.txt file2.txtgit loggit log -n 5git 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-05git log --grep="Init"Make the working directory file index.html look like the same version that is in my repository
git checkout -- index.htmlChange the index.html to the last commit (HEAD) of our current branch/repository (master)
git reset HEAD index.htmlIt 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 logWe 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.htmlGit revert create a new commit with the commit we choose from git log
git log
git revert 987e06a6d47b724223b40c2ccda229c58fa0b63fgit reset allow to specifie where the HEAD pointershould 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 987e06a6d47b724223b40c2ccda229c58fa0b63fgit reset --mixed 987e06a6d47b724223b40c2ccda229c58fa0b63fgit reset --hard 987e06a6d47b724223b40c2ccda229c58fa0b63f# Check what would happen
git clean -n
# Remove untracked file that are not in stage index
git clean -fWe 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.htmlGit ignores empty dirs
touch my_tracked_dir/.gitkeeptree-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