Skip to content

Instantly share code, notes, and snippets.

@ColtHands
Last active July 10, 2023 10:56
Show Gist options
  • Save ColtHands/fd6d884bf820f258ef2ea126b3adf4a4 to your computer and use it in GitHub Desktop.
Save ColtHands/fd6d884bf820f258ef2ea126b3adf4a4 to your computer and use it in GitHub Desktop.
Git Snippets

Git Snippets

How to remove file that was added to .gitignore after it was commited.

git rm {file_name} --cached

How to add changes to previous not pushed commit.

git commit --amend --no-edit

How to remove changes from specific file

git checkout -- {filename}

How to show diff changes between two commit hashes

git diff 8b33892 c5ecc5c

How to merge just a single file from one branch to another

git checkout {--patch} {branch} {filepath}

How to cherry pick a specific branch

  • Find log git log --oneline --graph
  • Cherry pick a range — git cherry-pick ebe6942..905e279

How to merge specific files from one branch to another

git checkout --patch branch-from path/to/file.js

How to change filename

git mv [old_name_with_path] [new_name_with_path]

How to remove git branch from local and remote

First remote from remote

git push -d <remote_name> <branch_name>

Then remove local

git branch -d <branch_name>

How to merge and autoresolve confilcts giving priority to merging branch

git merge branch --strategy-option theirs

Check what branches have commit

git branch -a --contains <commit>

How to add only part of the file

git add {filename} --patch

>
* y stage this hunk for the next commit
* n do not stage this hunk for the next commit
* q quit; do not stage this hunk or any of the remaining hunks
* a stage this hunk and all later hunks in the file
* d do not stage this hunk or any of the later hunks in the file
* g select a hunk to go to
* / search for a hunk matching the given regex
* j leave this hunk undecided, see next undecided hunk
* J leave this hunk undecided, see next hunk
* k leave this hunk undecided, see previous undecided hunk
* K leave this hunk undecided, see previous hunk
* s split the current hunk into smaller hunks
* e manually edit the current hunk
* You can then edit the hunk manually by replacing +/- by #
* ? print hunk help
<

How to remove all occurances of a file or a directory from git history (this creates new history and removes remote origin)

git filter-branch --tree-filter 'rm -rf node_modules' --prune-empty HEAD --force

Remove utracked files

git clean --force

Remove deleted(from remote) branches that show up in branch --all

git fetch --prune git pull --prune

Rename a branch

git branch -m {oldName} {newName}

Or when on the same branch git branch -m {newName}

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