git stash single file


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


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


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 
# view the short git hash of the last commit 
$ git rev-parse --short HEAD

git commands


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


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


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

View Git Stash Contents Without Applying to Codebase


Use git stash list to view all stashes

Use git stash show with options to view a stash without applying it to the codebase.

# see a "git diff" of what's in the first stash 
git stash show -p stash@{0}

And, a shell script to view these. Save this into a file, chmod +x the file, and run in the terminal.

readonly STASH="$1"
if [[ "xx${STASH}" != "xx" ]]; then
    git stash show -p stash@{${STASH}}
    git stash list

Hide .git repo with Apache


Solution 2.

Don't allow apache to serve anything from the .git repo that might be in the DocumentRoot . This is great because it hides that there even IS a git repo on the webserver.

Add this to the .htaccess in the DocumentRoot after the RewriteEngine On line.

RedirectMatch 404 /\.git

Git remove from repo, keeping on file system


git rm will remove a file from the file system and the repository. If you want to keep the file locally (say, because you're adding it to the .gitignore file), remove it from the repository cache:

# for a file
git rm --cached filename
# for a directory
git rm --cached -r directoryname

Unstage a file in git


Add a file / directory to staging

git add filePath

Now, if you want to remove it from stage, but want to keep the changes you've made locally.

# unstage the file, remove it from the index, don't remove it from the working directory
# a commit will remove it from the repo
$ git rm --cached filePath
# unstage a file, keep the local changes
$ git reset filePath