git

git stash single file

Snippet

If you want to stash a single file, and not the whole change set. use --

See also View Git Stash Contents Without Applying to Codebase

Of note, this will stash staged changes, too, even if you dno't specify them in the --

$ git stash -- filename.ext

git add untracked files

Snippet

Add all untracked files with one command.

git ls-files -o --exclude-standard lists untracked files.

git add $(git ls-files -o --exclude-standard)

Short git version hash

Snippet

Use rev-parse for the git hash, and the --short option for the short version of the hash.

# view the git hash of the last commit
$ git rev-parse HEAD 
1f0543ff172bdc5c5e90aac41c84103049f58390
 
# view the short git hash of the last commit 
$ git rev-parse --short HEAD
1f0543f

git commands

Snippet

Bunch of git commands I use frequently.

# add all the modified files to staging
git add `git st | grep modified | cut -c14-120`
 
# add the removal all the deleted files from the repo to staging
git rm `git st | grep deleted | cut -c14-120`

git reset of a single file

Snippet

git reset will discard any local changes you have in the current branch you have checked out.

Sometimes, however, you want to reset a single file, not the whole branch. In this case, use checkout, but use the special "option" --, which is unix for "treat every argument after this point as a file name, no matter what it looks like."

# general case
git checkout HEAD -- [file]
 
# my specific case used ALL THE TIME
git checkout HEAD -- package-lock.json

Apply branch changes without merging with Git

Snippet

You're working in one branch and you want the changes from another branch (say, you've done a pull request, and want the changes made in that other branch in your current branch), but don't want to merge the changes, you just want them available.

Assuming you want them all in one go, merge without committing, with a squash, then unstage the staged files that will come over with the merge.

If you want specific changes, use git cherrypick -n

git merge --no-commit --squash branch_with_commits
git reset HEAD

Pages