Skip to content

Instantly share code, notes, and snippets.

@StudioEtrange
Last active May 21, 2025 12:20
Show Gist options
  • Save StudioEtrange/0c72e98ca981e5013371cc80f4ad6460 to your computer and use it in GitHub Desktop.
Save StudioEtrange/0c72e98ca981e5013371cc80f4ad6460 to your computer and use it in GitHub Desktop.
Various git tips

Various git tips

store git password in a local file

git config --global credential.helper store
  • by default, password are stored in $HOME/.git-credentials

disable any credential helper

git config --system --unset credential.helper

see git log in color with graphs

git log --graph --oneline --all --decorate

image

  • get all remote branch (except HEAD) intto a local branch

    git fetch --all
    for remote in $(git branch -r | grep -v '/HEAD'); do
      branch=${remote#origin/}
      git branch --track "$branch" "$remote" 2>/dev/null
    done
    
  • update all local branch from remote

    for b in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
      git checkout "$b" && git pull --ff-only
    done
    git checkout -
    

Compress N last commits into one

  • Delete 4 last commits from git history, replace them with only one commit, and push this new history to git server

    git reset --soft HEAD~4
    
  • Create only one commit with the changes of previous deleted commits

    git add .
    git commit -m "one commit with content of 4 deleted commit"
    
  • Push this new history to git server

    git push --force -u origin main
    

Compress all commit history into one

  • Create a new branch with the content of all changes in git history into only one commit

    git branch new_branch $(echo "init message" | git commit-tree HEAD^{tree})
    
  • Push this new history to git server and replace the main branch

    git push --force -u origin new_branch:main
    
  • Delete new branch and replace main branch history with the new one

    git branch -d new_branch
    git fetch origin
    git reset --hard origin/main
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment