Check the status of all files:
git status -uall
Stage new files:
git add <file>...
Where <file> is the file path to add (this can be specified multiple times).
Stage new directories:
git add <dir>...
Where <dir> is the directory path to add (this can be specified multiple times).
Stage modified and removed files:
git add -u
Stage removed files, and delete from file-system:
git rm <file>...
Where <file> is the file path to remove (this can be specified multiple times).
Stage removed directories, and delete from file-system:
git rm -r <dir>...
Where <dir> is the directory path to remove (this can be specified multiple times).
Stage removed file, but keep file on file-system:
git rm --cached <file>...
Where <file> is the file path to remove (this can be specified multiple times).
Stage new and modified files, excluding removed:
git add .
Stage new, modified, and removed files:
git add -A
Stage new, modified, and removed files from one or more directories:
git add -A <dir>...
Where <dir> is the directory path to add (this can be specified multiple times).
Unstage all files:
git reset
Unstage one or more files:
git reset <file>...
Where <file> is the file path to unstage (this can be specified multiple times).
Unstage all files from one or more directories:
git reset <dir>...
Where <dir> is the directory path to unstage (this can be specified multiple times).
Revert one or more files:
git checkout -- <file>...
Where <file> is the file path to revert (this can be specified multiple times).
Revert all files from one or more directories:
git checkout -- <dir>...
Where <dir> is the directory path to revert (this can be specified multiple times).
Create a new local branch:
git checkout -b <branch> [<parent>]
Where:
- <branch> is the name of the new branch.
- <parent> optionally is where to branch from. Default is typically
master
.
Delete a local <branch>:
git branch -d <branch>
If that fails with:
error: The branch '<branch>' is not fully merged. If you are sure you want to delete it, run 'git branch -D <branch>'.
It means the changes from <branch> have not been merged. If this is not an issue, delete <branch> with:
git branch -D <branch>
Make sure local <parent> is up-to-date:
git checkout <parent> git pull
Merge <branch> into <parent>:
git checkout <parent> git merge --no-ff <branch> git push
Merge <branch> into <parent> without committing:
git checkout <parent> git merge --no-commit --no-ff <branch>
Delete merged <branch>:
git branch -d <branch>
Rebase <branch> on <parent>:
git checkout <branch> git fetch git rebase -p
Rename a local <branch> to <renamed>:
git branch -m <branch> <renamed>
Complete, fresh pull from master:
git clean -dxf git fetch --all git reset --hard origin/<branch>
Delete all untracked local files:
git clean -dxf
Delete a file from history:
git filter-branch --index-filter "git rm --cached --ignore-unmatch <file>"
Where <file> is the file path (relative to the git repository) to remove.
Merge <commit> from source branch into <target> branch:
git checkout -b merge-<commit> <target> git cherry-pick -x <commit> git checkout <target> git merge merge-<commit>
To merge multiple commits, perform an individual git cherry-pick
command per commit.