Skip to content

Instantly share code, notes, and snippets.

@jotelha
Last active August 7, 2019 14:13
Show Gist options
  • Save jotelha/742d05a40d8c660ddab823d9b8c2a1cd to your computer and use it in GitHub Desktop.
Save jotelha/742d05a40d8c660ddab823d9b8c2a1cd to your computer and use it in GitHub Desktop.

Useful git commands

View commits affecting the differences of certain files between two different branches:

git log --reverse -p getFileByQuery..master -- fireworks/utilities/filepad.py fireworks/user_objects/firetasks/filepad_tasks.py

Create a patch from these differences:

git log --reverse -p getFileByQuery..master -- fireworks/utilities/filepad.py fireworks/user_objects/firetasks/filepad_tasks.py > patch.diff

Cherry-pick changes in these commits:

git rev-list --reverse getFileByQuery..master --author='johannes' -- fireworks/utilities/filepad.py fireworks/user_objects/firetasks/filepad_tasks.py | git cherry-pick --stdin

Show commits of certain author for certain branch and file:

git rev-list --abbrev-commit master materialsproject --author="johannes" -- filepad_tasks.py
rev-list getFilesByQuery..master -- fireworks/utilities/filepad.py 
git rev-list --reverse getFileByQuery..master -- fireworks/utilities/filepad.py fireworks/user_objects/firetasks/filepad_tasks.py

Show applied changes in such a set of commits

git log -p master materialsproject --author="johannes" -- filepad_tasks.py
git log -p getFilesByQuery..master -- fireworks/utilities/filepad.py
cat commits.txt 

Show paralel development of branches

git log --graph --oneline --decorate --boundary master...materialsproject

Apply selection of commits in file

cat commits.txt | git cherry-pick --no-commit --stdin master materialsproject

Show only differences in files that exist in both branches:

git diff --diff-filter M materialsproject...master
#!/bin/bash
# git hook to get certain files from another repository
RELATED_REMOTE_REP="git@github.com:jotelha/mdtools-jlh.git"
REMOTE_NAME="mdtools"
# compare and update files of interest one-way:
REMOTE_BRANCH="master"
LOCAL_FILES=("NetCDF/ncfilter.py" "NetCDF/MPIFileHandler.py")
REMOTE_FILES=("mdtools/jlh/bin/ncfilter.py" "mdtools/jlh/bin/MPIFileHandler.py")
# add "unrelated" remote repository
echo "Adding remote repository '${RELATED_REMOTE_REP}' as '${REMOTE_NAME}, ignore error if already present..."
git remote add "${REMOTE_NAME}" "${RELATED_REMOTE_REP}"
# fetch branch of interest
echo "Fetching remote repository '${REMOTE_NAME}/${REMOTE_BRANCH}'..."
git fetch "${REMOTE_NAME}" master
NUPPER=$((${#LOCAL_FILES[*]}-1))
echo "Overriding $((${NUPPER}+1)) files..."
for i in $(seq 0 ${NUPPER}); do
LOCAL_FILE="${LOCAL_FILES[i]}"
REMOTE_FILE="${REMOTE_FILES[i]}"
# patch local file based on difference to remote file:
echo "Diffing local file '${LOCAL_FILE}' and remote file '${REMOTE_NAME}/${REMOTE_BRANCH}:${REMOTE_FILE}', applying patch..."
git diff "${REMOTE_NAME}/${REMOTE_BRANCH}:${REMOTE_FILE}" -- ${LOCAL_FILE} | patch -p1 --verbose --reverse "${LOCAL_FILE}"
echo "Adding locally patched file '${LOCAL_FILE}..."
git add "${LOCAL_FILE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment