Working copy (working tree file): It refers to the file in the repository that appears on the hard disk.
Index (staging area or cache): it refers to you have git add-ed, or, what would be committed if you were to run git commit.
HEAD: It refers to the “current” or “active” branch, when we need to check out a branch (referring to your attempt to match the branch with what is in the working copy), only one can be checked out at a time.
git restore server.go # Copies server.go from index to working directorygit restore server.go main.go # Restores multiple files in working directorygit restore . # Discards all local changes (except untracked files)git clean -fd # Removes all untracked files
# Restoring the working tree from the indexgit restore main.go
# Equivalent togit restore --worktree main.go
# Restoring index content from HEADgit restore --staged main.go
# Restoring the working tree and index from HEADgit restore --staged --worktree main.go
git log file.txt # Shows the commits that touched file.txtgit log --stat file.txt # Shows statistics (the number of changes) for file.txtgit log --patch file.txt # Shows the patches (changes) applied to file.txt
git show 921a2ff # Shows the given commitgit show HEAD # Shows the last commitgit show HEAD~2 # Two steps before the last commitgit show HEAD:server.go # Shows the version of server.go stored in the last commit
git log -3 # Shows the last 3 entriesgit log --author=crazyoptimist
git log --before="2021-01-01"git log --after="one week ago"git log --grep="GUI"# Commits with "GUI" in their messagegit log -S"GUI"# Commits with "GUI" in their patchesgit log hash1..hash2 # Range of commitsgit log file.txt # Commits that touched file.txt
git bisect start
git bisect bad # Marks the current commit as a bad commitgit bisect good ca49180 # Marks the given commit as a good commitgit bisect reset # Terminates the bisect session
git tag v1.0 # Tags the last commit as v1.0git tag v1.0 5e7a828 # Tags an earlier commitgit tag # Lists all the tagsgit tag -d v1.0 # Deletes the given taggit tag -a v1.0.0-rc1 # Creates an annotated taggit push origin v1.0.0-rc1 # Pushes the tag to origingit push origin --delete v1.0.0-rc1 # Deletes the tag from origingit push --tags # Push all tags
git switch dev # Switch to branch `dev`git switch -c dev # Create and switchgit switch - # Switch to the previous branchgit switch -d master~1 # Switch to the commit `master~1`
git stash push -m "some comments"# Creates a new stash. Without `push`, does the same.git statsh pop 1# Apply and remove the given stash from stash stackgit stash list # Lists all the stashesgit stash show stash@{1}# Shows the given stashgit stash show 1# shortcut for stash@{1}git stash apply 1# Applies the given stash to the working dirgit stash drop 1# Deletes the given stashgit stash clear # Deletes all the stashes
git merge bugfix # Merges the bugfix branch into the current branchgit merge --no-ff bugfix # Creates a merge commit even if FF is possiblegit merge --squash bugfix # Performs a squash mergegit merge --abort # Aborts the merge
git pull # Fetch & merge current remote branch into the current local branchgit pull --rebase # Fetch & rebase current remote branch into the current local branchgit push origin master # Pushes master to origingit push # Shortcut for "git push origin CURRENT_BRANCH"
git reset --soft HEAD^ # Removes the last commit, keeps the changes stagedgit reset --mixed HEAD^ # Unstages the changes as wellgit reset --hard HEAD^ # Completely removes the changes