Add to stash
git stash
Add to stash including untracked
git stash -u
Add to stash including untracked and ignored
git stash -a
Revert last stash
git stash pop or git stash apply && git stash drop
FYI - git stash pop is git stash apply && git stash drop
List all stashed
git stash list
See last stash files changed
git stash show
See last stash files changed including content changes
git stash show -p
Create a branch with a stash
git stash branch some-stash-branch stash@{1}
Clear all stashes
git stash clear
Undo last commit to stage
git reset --soft HEAD^
When you use --soft parameter: Git will reset all uncommitted changes. But changes are left staged (index).
When you use --mixed (default)parameter: Git will reset all uncommitted changes as well as unstaged changes. But changes are left in working tree.
When you use --hard parameter: Git will reset all uncommitted changes, unstaged ones, as well as deleted changes, nothing's left.
When you use --merge: Resets the index and updates the files in the working tree that are different between and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between and the index has unstaged changes, reset is aborted. In other words, --merge does something like a git read-tree -u -m , but carries forward unmerged index entries.
When you use --keep: Resets index entries and updates files in the working tree that are different between and HEAD. If a file that is different between and HEAD has local changes, reset is aborted.
Discard all local changes in working tree
git reset --hard HEAD
Reset your HEAD pointer to a previous commit and preserve changes as unstaged changes ( Default git reset is mixed)
git reset <commit>
Undo all unstaged files
git checkout -- .
Undo a commit (by producing a new commit with contrary changes)
git revert
Undo a commit to unstage (by producing a new commit with contrary changes)
git revert -n
Squash 2 commits into one
git rebase -i HEAD~2
GIT maintains following three trees:
HEAD: This is the last commit snapshot. Index: This is the proposed next commit snapshot. Working Directory: This is the sandbox for doing changes.
Remove branches that no longer exist on remote
git fetch -p
Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo git push <remote> --tags
Stop tracking a tracked file git rm --cached filename.xyz
Branch is newly created, then you also need to upload the branch with the following command:
git push --set-upstream <remote> <name-of-your-branch>
or git push -u origin <branch_name>
Delete branch locally git branch -d localBranchName
Delete branch remotely git push origin --delete remoteBranchName
or git push origin :<branch>
Get commits git pull
or git fetch && git merge FETCH_HEAD
Update last commit message git commit --amend
View untracked files that will be remote git clean -n
and Remove untracked files git clean -f