Git#
Git is powerful and obscure at the same time. You'll find here some snippets I found useful.
Table of Contents :
Remote repository#
- Show the current remote repo :
git remote -v
- Delete the current remote repo :
git remote remove origin
- Add a new remote repo :
git remote add origin <repository>
Note that remote repository can be hosted anywhere, for instance :
Working together, on the same repo#
git stash
to set aside my code.git pull
to get the latest team's code.git stash pop
to get back my stashed code.
Commits#
-
Cancel the last commit
git reset --soft HEAD~1
-
Find a commit by a file content
git log -S "your search"
. Then add a--patch
to show the modifications, like so :git log -S "your search" --patch
-
Find a changed filename
git log -p --follow -- <filename>
, example :# list all commits related to *story.md (like history.md) git log --follow -- "*story.md" # jump in a specific commit git checkout 6bab9f43f57fffe7d79f700416cc05b7daaadf64 # find the file find -iname "*story.md" # display its content cat exercice/history.md
Branch#
-
Create a new branch (and jump in it)
git checkout -b <branch-name>
-
Jump to another branch
git checkout <branch-name>
-
Jump to a branch that only exists on the remote
git checkout --track remotes/origin/your-incredible-branch-name
-
Locally reference all remote branches :
for BRANCH in $(git branch -a | grep remotes | grep -v HEAD | grep -v master); do git branch --track "${BRANCH#remotes/origin/}" "${BRANCH}"; done
-
Listing differences between two branches
git diff <branch-one> <branch-two>
-
Merging a branch with another one
# jump in the receiving branch git checkout main # then import a specific branch git merge <branch-name>
-
Get the latest commits from a specific branch
git pull origin <branch-name>
Useful VSCodium extension#
- Git Graph - display branches, list changes, checkout to a commit, compare commits, filter, etc. (also available on M$ visual studio "marketplace" 🤢)