Skip to content

Instantly share code, notes, and snippets.

@paulo-raoni
Last active August 7, 2020 21:37
Show Gist options
  • Save paulo-raoni/9bfecb0e686c939f6a43016b7c663851 to your computer and use it in GitHub Desktop.
Save paulo-raoni/9bfecb0e686c939f6a43016b7c663851 to your computer and use it in GitHub Desktop.
Git Helper Commands

Commands (alternatives) to delete all branchs (locally) except master and developer:

git branch | grep -v "master\|developer" | xargs git branch -D

git branch -D git branch | grep -vE 'master|developer'

Command to delete remote branch:

git push origin --delete <branch-name>

Stash and stash pop to a new branch created on the fly:

Using stash:

  1. git stash

  2. git stash branch <branch-name>

  3. And then, automatically goes to the new created branch

Using checkout:

  1. git checkout -b <branch-name>

Change remote URL:

git remote -v (view existing remotes)

git remote set-url origin https://github.com/user/repo2.git (change the 'origin' remote's URL)

Stop tracking and ignore changes to a file in Git (without .gitignore):

git update-index --skip-worktree <path-name>

To undo the ignore use instead git update-index --no-skip-worktree <path-name>

This is to tell git you want your own independent version of the file or folder. For instance, you don't want to overwrite (or delete) production/staging config files.

Tip 1: To ignore multiple files, just write every path separated by a white space.
Tip 2: To see all ignored files, type git ls-files -v | grep ^S in the projects terminal.

Undo commit

When using git reset --soft HEAD~1 you will remove the last commit from the current branch, but the file changes will stay in your working tree.

Best explained in this stackoverflow answer: https://stackoverflow.com/a/24569160/13325505

Add further files to the current commit already committed

git add file (or git add . to add all files).

And then git commit --amend to inform that the files are going to be added to the current commit

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