Local changes
Show Changes
Show changes in your working directory.
git status
Show File Differences
Show all differences of what is changed but not staged.
git diff
Handle Changes
Add Changes for Next Commit
Add all tracked and untracked files or just a specific file to your next commit (stage).
git add <file_or_directory>
Commit Changes
Commit your changed content as a new commit.
git commit -am "<descriptive message>"
Amend last message
Change last commit message.
git commit --amend "<descriptive message>"
Don't amend pushed commits
Rebase
Rebase the current branch onto <base>
, where <base>
can be a commit ID, branch name or tag.
git rebase <base>
Don't rebase pushed commits
Explicit versioning a file remove
Delete the file from repo and stage the removal for commit.
git rm <file>
Undo Changes
Undo last Changes
Discard all changes,
git reset HEAD
Unstage a file while retaining the changes.
git reset <file-name>
Discard any local, uncommitted changes and thereby restore their last committed state.
git restore .
Revert
Use this command to undo changes to a repository's puplished commit history. The other undo-commands such as, git checkout or git reset, move the HEAD/branch ref pointers to a specified commit. The revert
operation will take the specified commit, inverse its changes and creates a new & reverted commit.
git revert <SHA>
Show Commit History
Show the commit history for the currently active branch, starting with the newest.
git log
Show Git Object
Show any object in Git <SHA>
in human-readable format.
git show <SHA>
Temporary commits
This is useful to temporarily store modified files (on a stash stack) in order to change branches. For example when pulling into a dirty working directory (1)
- Example pulling to dirty directory
$ git pull ... file foobar not up to date, cannot merge. $ git stash $ git pull $ git stash pop
Save modified and staged local modifications away and revert the working directory.
git stash
List stack-order of stashed file changes.
git stash list
Re-apply stored changes from top of the stash stack.
git stash pop
Discard the changes from top of the stash stack.
git stash drop