Git

  • Using bisect to find bugs

    git bisect start
    git bisect good <sha1>  # mark commit as good
    git bisect bad <sha1>  # mark commit as bad
    git bisect run <cmd>  # run cmd automatically to find bad commit
    
  • (GitHub) How to recover the deleted branch of a PR?

    git clone git@github.com:<username>/<repo>.git
    git fetch origin pull/<pr_id>/head:<new-branch>
    git checkout <new-branch>
    
  • Moving specific directories to a new repository

    mkdir <new-repo>
    cd <new-repo>
    git init
    git remote add <old-repo> git@github.com:<username>/<old-repo>.git
    git fetch <old-repo>
    git merge <old-repo>/<master> --allow-unrelated-histories 
    # Go to next bullet "Isolate commits..."
    
  • Isolate commits related to specific directories using git-filter-repo –path

    sudo apt install git-filter-repo
    git filter-repo --path <path1> --path <path2> --path <pathN>
    
  • Merging multiple repositories (–allow-unrelated-histories) (Tweet)

    cd <project-a>
    git checkout -b merged
    git remote add <project-b> git@github.com:<username>/<project-b>.git
    git fetch <project-b> -a --tags
    git merge --allow-unrelated-histories <project-b>/<master>
    
  • Revert only one file (Tweet)

    git checkout <commit_hash>~1 -- <path/to/file>
    git commit
    
  • Search for a commit in a deleted branch (Tweet)

    git reflog --no-abbrev
    
  • Blame history before a commit (Tweet)

    git blame <commit_hash>^ -- <path/to/file>
    
  • Search for a deleted code (Tweet)

    git log -c -S'removed_code' <path/to/file> # search for string
    git log -c -G'removed.*code' <path/to/file> # search for regex
    
  • Edit a commit (Tweet)

    git rebase -i '<commit_hash>^'
    # Change 'pick' to 'edit' in the commit to be edit.
    # Update the files.
    git commit --all --amend --no-edit
    git rebase --continue
    
  • Undo a commit (uncommit) (Tweet)

    git reset --soft HEAD^
    
  • Add changes to the last commit (Tweet)

    git commit --amend
    
  • How to find a file that I don’t know on which branch is (Tweet)

    git log --all -- <file-path>
    
  • Better git log format

    git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
    git lg
    

    git lg, a better log

  • Add some parts of a file (Tweet)

    git add -p
    
  • Pull a branch squashing all remote commits (Tweet)

    git pull --squash origin <branch>
    
  • Rewrite history updating commits author name and e-mail

    git filter-branch --env-filter '
    	OLD_EMAIL="<the old e-mail>"
    	CORRECT_NAME="<the new name>"
    	CORRECT_EMAIL="<the new e-mail>"
    
    	if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
    	then
    		export GIT_COMMITTER_NAME="$CORRECT_NAME"
    		export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    	fi
    	if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
    	then
    		export GIT_AUTHOR_NAME="$CORRECT_NAME"
    		export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    	fi' -- origin/HEAD..HEAD
    
  • Fixing down merge conflicts

    export FROM=<branch>
    export TO=<branch>
    git checkout $TO
    git pull --ff-only origin $TO
    git branch -D downmerge-$FROM-to-$TO
    git checkout -b downmerge-$FROM-to-$TO
    git pull origin $FROM
    # fix conflicts
    git commit
    git push origin downmerge-$FROM-to-$TO
    

Trunk based Link to heading

Versioning Link to heading