Git Reminder
One very good tutorial to check and to grow. Quick reference
- To create a local branch and set remote tracking:
git switch -c <branch>with<branch>being also a remote origin or it creates it -
git pullmakes in this ordergit fetchto fetch remotes changes andgit mergeto merge them in local branch so be careful => better to just fetch - in case of destructing the project (test before merging to main especially in CI/CD context…)
git switch -c recovery-branch <commit_hash>=> creates the new branch (fail if branhc-name already exists, -C to overwrite) -
$ git switch --detach HEAD~3to go to the commit (third previous) - in case of failure of a
git switch maingit merge <branch-name> #into main=>- fix conflicts and run
git commit. - Use
git merge --abortto abort the merge.
- fix conflicts and run
- oh.. forking is all about separating the push repo from the fetch repo
-
git fetch --pruneto remove remote branches that have been deleted remotely -
git push origin --delete topic99remote tracking branch on the repo - Changing the git states of the files:
- Untracked →
git add foo.txt→ Staged (as “new file”) - Modified →
git add foo.txt→ Staged - Modified →
git restore foo.txt→ Unmodified - Unmodified → edit foo.txt → Modified (with your favorite editor)
- Staged →
git commit→ Unmodified - Staged →
git restore --staged foo.txt→ Modified #allow to remove the file from the commit
- Untracked →
- Example to remove the file from the repo but keep it locally
$ ls
foo.txt
$ git rm --cached foo.txt
rm 'foo.txt'
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: foo.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
foo.txt
$ ls
foo.txt
- Multiple states at ones: check here
- To diff the staged version with the previous commit:
git diff --staged - Reminder: A
git statusorgit diffonly shows files that differ between your working tree and the stage. - ACHTUNG: git diff always compares the working tree to the stage.
- git diff can compare two branches:
$ git diff main topic -
git mv foo.txt bar.txtto rename files (no more git rm, add…) -
git restore foo.txtafter a staged renaming with mv to restore and clean status (otherwise withgit restore --stagedmakes agit rm) - if restoring with a specific commit:
$ git restore --source=1c9bf foo.txtbe careful about adding it back and committing it ! - [typo] for typo
Enjoy Reading This Article?
Here are some more articles you might like to read next: