Skip to content

Instantly share code, notes, and snippets.

@cpburnz
Last active April 26, 2017 16:25
Show Gist options
  • Save cpburnz/e7b559eeecb8827abca3 to your computer and use it in GitHub Desktop.
Save cpburnz/e7b559eeecb8827abca3 to your computer and use it in GitHub Desktop.
Git notes.

Git Notes

Staging

File status

Check the status of all files:

git status -uall

New files

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).

Modified files

Stage modified and removed files:

git add -u

Remove files

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).

All files

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 files

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 files

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).

Branches

Create branch

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 branch

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>

Merge 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

Rebase <branch> on <parent>:

git checkout <branch>
git fetch
git rebase -p

Rename branch

Rename a local <branch> to <renamed>:

git branch -m <branch> <renamed>

Resetting

Complete, fresh pull from master:

git clean -dxf
git fetch --all
git reset --hard origin/<branch>

Delete all untracked local files:

git clean -dxf

History

Delete File

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.

Miscellaneous

Merge specific commits

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment