Skip to content

Instantly share code, notes, and snippets.

@jportella93
Last active May 31, 2019 18:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jportella93/877aaeade674621a3ace5c4515c5d1fc to your computer and use it in GitHub Desktop.
Save jportella93/877aaeade674621a3ace5c4515c5d1fc to your computer and use it in GitHub Desktop.
Some useful git commands and git wisdom in general.

Add --patch all files which name includes 'foo'

git add -N . && git add -p "./*foo*"

Add --patch all files including untracked ones

git add -N . && git add -p

List all commits for a specific file

git log --follow -- <filename>

Pull remote branch to local branch

git fetch <remote> <rbranch>:<lbranch> 
git checkout <lbranch>

Get a specific file from stash

git checkout stash@{0} -- <filename>

Diff a file from stash

git diff stash@{0}^1 stash@{0} -- <filename>

Get a file from an older commit

git checkout <commit> <path/to/file.ext>

Modify old local commit

(Don't do it with public commits, only for local. It rewrites history.)

  1. Do a new commit fixing the old commit.
  2. Rebase until a previous commit in history.
git rebase -i <commit>
  1. In the text editor, move the new commit just under the want you want to modify and change pick for squash. For moving lines in vim use dd to cut the line and p to paste it.

Diff between two files in the filesystem

git diff --no-index <path/to/file1.json> <file2.json>

Only stash changes in a directory

git stash push -- <path/to/directory>

Push local repo to a new remote repo (bitbucket)

cd </path/to/my/repo>
git remote add origin ssh://git@bitbucket.org/<username>/<reponame>.git
git push -u origin --all

Squashing

To squash the latest 3 commits:

git rebase @~3 -i 

In vim pick the last one and change all others to s :2,20s/pick/s/g

Pushing to a remote branch with different name

git push origin <local-name>:<remote-name>

Delete all local branches except some

Test it first:

  1. Change master by the name of the branch you want to keep, the branches showing will be eliminated
git branch | grep -v "master"
  1. Eliminate branches with xargs
git branch | grep -v "master" | xargs git branch -D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment