Skip to content

Instantly share code, notes, and snippets.

@himalay
Last active October 23, 2022 22:58
Show Gist options
  • Save himalay/f8395d693342affde10e7e76232fe9ea to your computer and use it in GitHub Desktop.
Save himalay/f8395d693342affde10e7e76232fe9ea to your computer and use it in GitHub Desktop.
[git]
### get all branch
git branch -a | grep -v HEAD | perl -ne 'chomp($_); s|^\*?\s*||; if (m|(.+)/(.+)| && not $d{$2}) {print qq(git branch --track $2 $1/$2\n)} else {$d{$_}=1}' | csh -xfs
#### GET URL FROM LOCAL REPO
git config --get remote.origin.url
#### change git remote url
git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
git clone -b betav1 [email protected]:krishontreat/pain_diary.git
# RESTORE PREVIOUSLY DELETED FILE
git checkout HEAD~N -- file6.jpg
Where N is the number of commits back in time where file6.jpg was present.
#### CHECKOUT SPECIFIC FOLDER FROM GIT REPO
svn checkout http://github.com/<username>/<repoName>/trunk/<subfolder/s>
#### UNSTAGE
git reset HEAD -- path/to/file
git reset HEAD -- .
#### RENAME BRANCH
# If you want to rename a branch while pointed to any branch, simply do :
git branch -m <oldname> <newname>
# If you want to rename the current branch, you can simply do:
git branch -m <newname>
#### CREATE ARCHIVE
# Format: git archive {branchname} --format={compression} --output={filename}
git archive master --format=tar --output=kuma.tar
git archive some-feature-branch --format=tar --output=kuma.tar
## find and remove file from all commits
git filter-branch --tree-filter <command>
git filter-branch --tree-filter 'rm -f tiapp.xml' -- --all
git filter-branch --tree-filter 'find . name "*.mp4" -exec rm {} \;'
# --all filter all commits in all branches. HEAD filter only current branch
## move file to new loation and rewrite history
git filter-branch --tree-filter 'if [ -f my_file.cs ]; then mkdir -p new_folder; mv my_file.cs new_folder/; fi' HEAD
# move file and maintain their history
git filter-branch -f --tree-filter 'if [ -f a_file ]; then mv a_file new_dir/; fi' HEAD
# open all merge conflict files in code
git diff --name-only | uniq | xargs code
# or add alias
git config --global alias.cf '!sh -c "git diff --name-only | uniq | xargs code"'
# copy all files related to a commit to a folder recursively
for x in `git diff-tree --no-commit-id --name-only -r ba8e`; do cp --parents $x ~/out/; done
# loop through multiple commit and print files
for x in `echo bb830a2,5ed547e | tr ',' "\n"`; do echo ">>$x" && git diff-tree --no-commit-id --name-only -r $x; done
# check if file is tracked in git
git ls-files --error-unmatch <file name>
# assume unchanged - userful for config files
git update-index --assume-unchanged <file>
# undo/show dir's/files that are set to assume-unchanged
git update-index --no-assume-unchanged <file>
# list of dir's/files that are assume-unchanged
git ls-files -v|grep '^h'
# copy changed files between two commits to a location recursively
for x in `git diff-tree --no-commit-id --name-only -r SHA1 SHA2`; do cp --parents $x ~/out/; done
# Clone all repos of a organization or stared repo from github (4 git clones happening at the same time)
curl -s https://api.github.com/orgs/h5p/repos\?per_page\=200 | grep clone_url | awk -F '"' '{print $4}' | xargs -n 1 -P 4 git clone --depth=1
# https://api.github.com/users/himalay/starred
# Remove tracked files based on gitignore
git rm --cached `git ls-files -i --exclude-from=.gitignore`
#But this doesn't seem to work in Git Bash on Windows. It produces an error message. The following works better:
git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
# remove secrets from history
#https://help.github.com/articles/removing-sensitive-data-from-a-repository/
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch path/to/secrets.txt' \
--prune-empty --tag-name-filter cat -- --all
# Replace author name and email
git filter-branch -f --env-filter \
"GIT_AUTHOR_NAME='Abc'; GIT_AUTHOR_EMAIL='[email protected]'; \
GIT_COMMITTER_NAME='Xyz'; GIT_COMMITTER_EMAIL='[email protected]';" HEAD
# diff of a file since x days
git diff $(git log -1 --before=@{3.days.ago} --format=%H file.txt)
# Set the date of an arbitrary commit to an arbitrary or current date. Rebase to before said commit and stop for amendment:
git rebase <commit-hash>^ -i
# Replace pick with e (edit) on the line with that commit (the first one)
# quit the editor (ESC followed by :wq in VIM)
GIT_COMMITTER_DATE="Wed May 13 06:58:27 2020 +0545" git commit --amend --no-edit --date "Wed May 13 06:58:27 2020 +0545"
git rebase --continue
# change old commit date
git rebase -i commit_hash
export GIT_COMMITTER_DATE="`date -Rd '3 hour ago'`"
git commit --amend --no-edit --date "$GIT_COMMITTER_DATE"
git rebase --continue
unset GIT_COMMITTER_DATE
# change last commit date
GIT_COMMITTER_DATE="`date -Rd '3 hour ago'`" git commit --amend --no-edit --date "$GIT_COMMITTER_DATE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment