Skip to content

Instantly share code, notes, and snippets.

@refo
Last active September 4, 2018 11:44
Show Gist options
  • Save refo/3c6e6d63c531f06c818bbac701142f2a to your computer and use it in GitHub Desktop.
Save refo/3c6e6d63c531f06c818bbac701142f2a to your computer and use it in GitHub Desktop.
Git Command Snippets

Clone single branch and latest commit only

git clone --single-branch --depth 1 -b BRANCH REPO

Compate two branches and print what's merged into first branch ahead of the second branch

LEFT="branch-1"; \
RIGHT="branch-2"; \
git log "${LEFT}" HEAD...`git log "${RIGHT}" -1 --pretty=format:"%h"` --pretty=format:"%s" | grep "into ${LEFT}" | uniq | grep  -o "'.*'"

one liner

LEFT="branch-1"; RIGHT="branch-2"; git log "${LEFT}" HEAD...`git log "${RIGHT}" -1 --pretty=format:"%h"` --pretty=format:"%s" | grep "into ${LEFT}" | uniq | grep  -o "'.*'"

Last commit abbrv. hash on "prod" branch

git log prod -1 --pretty=format:"%h"

Print changed file names

git diff --name-only

# Use relative paths
git diff --name-only --relative

# Print changed files on last commit
git diff --name-only --relative HEAD^1 HEAD

Copy changed files

git diff --name-only --relative \
| rsync -R -d --files-from - ./ /target/dir

Remove untracked files from the working tree

git clean -f

# dry-run
git clean -f -n

# remove directories
git clean -fd

http://stackoverflow.com/questions/61212

Remove ignored files from index

git rm -r --cached .
# commit here to remove files from repository

# then, add your tracked changes and work as normal
git add .

Tag a commit from history

git tag -a 16.09.22 02f0e3ae36f3ba49ce2728f07d24a22d90d71e92

# push all new tags
git push --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment