Git
Remote: error: failed to lock refs/heads/…
Section titled “Remote: error: failed to lock refs/heads/…”Ignoring files without adding them to .gitignore
Section titled “Ignoring files without adding them to .gitignore”To ignore untracked files, you have a file in your git folder called .git/info/exclude. This file is your own gitignore inside your local git folder, which means is not going to be committed or shared with anyone else. You can basically edit this file and stop tracking any (untracked) file.
My basic configuration for a new device
Section titled “My basic configuration for a new device”git config --global user.name "Claus Conrad"git config --global user.email XXXXXXXXX+cconrad@users.noreply.github.comgit config --global core.autocrlf falseGet list of accessible repositories
Section titled “Get list of accessible repositories”ssh git@git.bekey.dk info- Source: Serhii Ch.
Pull and merge remote branch “origin/main” into current
Section titled “Pull and merge remote branch “origin/main” into current”git pull origin mainRollback to previous commit
Section titled “Rollback to previous commit”Revert your repo to an older commit and preserve all intervening work:
git checkout 307a5cd # check out the commit that you want to reset togit checkout -b fixy # create a branch named fixy to do the workgit merge -s ours master # merge master's history without changing any filesgit checkout master # switch back to mastergit merge fixy # and merge in the fixed branchgit push # done, no need to force push!Done! Replace 307a5cd with whatever commit you want in your repo.
Source: Stack Overflow
How to use git stash
Section titled “How to use git stash”Think of Git stash like a pocket where you can quickly put away your work when you need to switch to something else.
Imagine you’re working on a craft project (your code changes) and suddenly your mom calls you to help with groceries. You need to stop what you’re doing, but you don’t want to lose your progress.
With Git stash:
- You put your unfinished work in your pocket (
git stash save) - Go help with groceries (switch branches, pull updates, etc.)
- When you’re done, you take your work back out of your pocket (
git stash applyorgit stash pop)
The main commands are:
git stash save "message"- Put your changes away with a notegit stash list- See all your pockets of saved workgit stash apply- Take out your work but keep a copy in your pocketgit stash pop- Take out your work and empty that pocketgit stash drop- Throw away what’s in your pocket
It’s super useful when you need to quickly switch context without committing half-finished work!
Ignoring commits that only reformatted code during git blame
Section titled “Ignoring commits that only reformatted code during git blame”- Add the commit ID to
.git-blame-ignore-revs - Use
git config blame.ignoreRevsFile .git-blame-ignore-revsto enable it by default
Delete local branches merged to main
Section titled “Delete local branches merged to main”git branch --merged main | grep -v "^\*\| main" | xargs git branch -dWorktree cheatsheet
Section titled “Worktree cheatsheet”| Function | Example |
|---|---|
| Start working on a branch in a new directory | git worktree add DIRECTORY BRANCH |
| List all linked directories | git worktree list |
| Remove a worktree | git worktree remove DIRECTORY |
| Create a branch and worktree at the same time, from outside the main repo location | git -C SOURCE_FOLDER worktree add TARGET_FOLDER -b NEW_BRANCH |
Resources
Section titled “Resources”- Introduction to GitHub and Open-Source Projects > How To Create a Pull Request on GitHub
- Git Forks and Upstreams: How-to