Show commits from another branch not contained in current branch
git cherry -v otherbranch
+ f7d6a569bb6912aac97fce9ac92c4302863fb0d9 thecommit
Cherry pick without commit
git cherry-pick -n HASH
Using vimdiff for diff
git config --global diff.tool vimdiff
git config --global merge.tool vimdiff
[diff]
tool = vimdiff
[merge]
tool = vimdiff
Using vscode as diff and mergetool
You need to have the shell integration installed (code binary in PATH) |
Update your global git config via git config --global -e
or by editing your ~/.gitconfig
:
[core]
editor = code --wait
[diff]
tool = vscode
[difftool "vscode"]
cmd = code --wait --diff $LOCAL $REMOTE
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code --wait $MERGED
Migrate master to main branch
git checkout master
git branch -m master main
git push -u origin main
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
in addition when using gitlab/github/bitbucket …
-
change default branch to
main
-
enable branch protection for
main
-
change existing merge requests with target = master
then delete master branch:
git push origin --delete master
== My dirt quick add and commit Alias
I use it for intermediate steps, that I am rebasing afterwards
git config --global alias.wip '!git add -A && git commit -m "wip"'
Revert last, already pushed commit
Let’s say we have to commits, both pushed in branch mybranch
..
We’re in branch mybranch
:
-
2fedaae377a54027712b0e4bd09a8ff5901e0b99 (HEAD)
-
9016822e4341ea2544e7625862c027440d466060
git push -f origin 9016822e4341ea2544e7625862c027440d466060:mybranch
git reset --hard 9016822e4341ea2544e7625862c027440d466060
Restore a deleted file
-
Take a look at the entire history for the file and note the commit you need:
git log --follow -p -- src/test/resources/import_file_muster.bin
-
Cherry Pick the changeset without creating a commit (-n or --no-commit)
git cherry-pick -n HASH
-
Eventually unstage modifications you do not want to keep …
git reset HEAD file/paths
-
Commit your changes ….
git commit -m "...."
Show changes in a dedicated date range
git log --since=4.days --until=3.days
Move existing Tag pushed to remote to another Commit
Assuming that the tag has already been pushed to remote .. we’re doing …
-
delete the tag local
-
delete the tag remote
-
tag specific commit
-
push tag remote
git tag -d <tagname>
git push origin :refs/tags/<tagname>
git tag <tagname> <commithash>
git push origin <tagname>
Git Deactivate Fast-Forward-Merges per Default and per Branch
Using git config:
git config branch.master.mergeoptions "--no-ff"
Setting manually in the git configuration:
[branch "master"]
mergeoptions = --no-ff
Git Contributor Stats for a given Period
git shortlog -s --number --since='last 2 year'
Convenience Alias for Force-With-Lease Push
git config --global alias.please "push --force-with-lease"
Remove sensitive file from Git history
git filter-branch –force –index-filter ‘git rm –cached –ignore-unmatch path/sensitive-file‘ cat — –all
// force push
Search for occurance in a commit
git log --follow -S 'term' -- path/to/file
Merge and override with foreign branch changes
git merge --strategy-option theirs BRANCHNAME
Git Syncing with forked Repository
git remote -v
git remote add upstream git@github.com:ORIGINAL_OWNER/PROJECT.git
git remote -v
git fetch upstream
git checkout master
git merge upstream/master
Git ignore versioned files
git update-index --assume-unchanged /path/to/file
Git Diff across Branches
git diff HEAD...other/branch -- path/to/file
Git setup a server using the git protocol
cd projectdir
git daemon --reuseaddr --base-path=. --export-all --verbose
git clone git://ipaddress/ appname
Git Fix too long filenames error on windows
git config --global core.longpaths true
Remove untracked files from the working tree
Revert and reset specific change
- Revert
-
Create a new commit that reverts the given commit
- Reset
-
Rewrites the history .. force push necessary
git reset --hard HEAD~2
git revert XXXXXX
Use global git ignores
-
Put your global ignores in
~/.gitignore
-
Use this file as global ignore list either by …
-
… running
git config --global core.excludesfile ~/.gitignore
-
… or by adding the following section to your
~/.gitconfig
[core] excludesfile = ~/.gitignore
-
My git config
[user]
name = Micha Kops
email = anonymized
[alias]
st = status
ci = commit
please = push --force-with-lease
wip = !git add -A && git commit -m \"wip\"
[core]
eol = lf
autocrlf = input
longpaths = true
excludesfile = ~/.gitignore
[diff]
tool = vimdiff
[merge]
tool = vimdiff
Git Aliase for my Shell
# git reset
alias gitr='git reset'
alias gitrh='git reset --hard'
# reset to origin branch with same name as local branch
alias gitrho="git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)"
Shell Script to print Git Hash of repositories in directory
#!/bin/bash
repo_directory="/Users/me/path/to/repositories"
# Change to the repository directory
cd "$repo_directory" || exit
# Loop through each repository
for repo in */; do
# Move into the repository directory
cd "$repo" || exit
# Get the repository name
repo_name=$(basename "$repo")
# Get the current branch and commit hash
branch=$(git rev-parse --abbrev-ref HEAD)
commit_hash=$(git rev-parse HEAD)
# Print the summary
echo "Repository: $repo_name, Branch: $branch, Commit Hash: $commit_hash"
# Move back to the parent directory
cd ..
done