GIT/shaare/kW0W5g
git init→ initialize a new Git repogit clone→ clone remote repogit add→ stage change for next commitgit commit→ create new commit with staged changesgit push→ upload local commits to a remote repogit pull→ download change from remote to localgit checkout→ switch between branches or restoregit merge→ combine change from branches
Installing Git
git config --globaluser.name→"your name"user.email→"my@email.com"- preferred text editor
core.editor→"code --wait"- color output
color.ui→auto
git status→ check the current stategit log→ commit history
Branching and Merging
-
git branch→ create a new branch -
git checkout main -
git merge mybranch
git init → create new repository
-
git init -
git init --bare→ no working directory- stocker l'historique Git et synchronise l'équipe
-
git init --template=/path/to/template/dir- use configuration in directory
- for new Git folder
git add
-
git add→ stage changes for next commit- staging area →
git status
- staging area →
-
git add [tag] -
git add . -
git add *.txt
git reset [tag]→ reset
.gitignore
- Create a
.gitignorein root of repo - Specify pattern of file you want to ignore
*.log.DS-Store
git commit → snapshot → commit hash
1st:
git add .git status
2nd:
git commit -m "my message"
3rd:
git log→ show commit history
Amend Commit → modify the most recent commit
-
Change the commit message
-
Add or remove file from the commit
-
Correct mistake or typos
-
git commit --amend -m "typo fix"
Undo Commit
-
git reset HEAD~1→ most recent commit -
git reset HEAD~3→ last 3 commit -
git revert→ new commit that undo the change
Cloning a Repository
-
git clone https://www..../repository.git -
git status→ check current branch -
git push origin master- push change to remote repo
-
git pull→git fetch + git merge -
git fetch→ retrieve the latest change from remote -
git merge→ merge the changes from the remote directory
Creating Branches
-
git branch feature/new-login-page- create branch
-
git checkout feature/new-login-page- switch to new branch
-
git checkout -b feature/new-login-page- create new branch & switch to it
-
git checkout master- switch back to master branch
Merging Branches
git merge feature/new-login-page
Best Practices
-
Use consistent branch naming
feature/bugfix/hotfix
-
Keep branches small and focused on a single feature
-
Regularly merge branch
-
Delete merged branch
-
Synchronize local branch with the remote repo
Git Flow Workflow
Main Branches
master→ production ready codedevelop→ dev code with new features
Supporting Branches
feature→ dev new featurerelease→ use to prepare a new releasehotfix→ quick fix of critical bug
-
Start new feature
- branch from
develop
- branch from
-
Work on new branch & commit
-
Merge feature back to
develop -
Create a release
- branch off
develop release/1.2.0
- branch off
-
Finalize release
- make change
- merge release into
master develop
-
Fix a bug
- branch off
master hotfix/1.2.1- merge
- branch off
Pull Request Workflow
- Branch out → create new branch from master
- Commit change → commit your work to new branch
- Push the branch → push branch to the remote
- Open a pull request → go to web interface, create pull request
- Add a description → describe change you've made
- Review → assign team members to review the pull request
Rebasing
- Rewrite the commit history
Example
-
master→A --- B --- C -
feature→D --- E -
Faire partir la branche feature de
C -
git checkout feature -
git rebase master -
git push --force- pousse avec force l'historique
-
Resulting feature branch:
A --- B --- C --- D --- E
-
git rebase --interactive- customize rebase concept
Stashing Changes
-
Save local change without committing them
-
Create a snapshot of your current working dir
-
git stash -m "WIP: implementing new feature" -
git stash list- list of all stash changes
-
git stash show stash@{0}- show change of a specific stash
-
git stash apply- apply most recent stash
-
git stash drop- remove stash
-
git stash clear- remove all stash
-
git stash pop- apply & drop
Stash Branching
git stash branch new-feature stash@{0}- create new branch
"new-feature" - checkout the new branch
- apply change from the stash
- drop the applied stash
- create new branch
Git Hooks
-
Script that run automatically every time an event is made in repo
-
Automate task
-
pre-commit hook→ run before commit -
commit-msg hook→ run after commit -
pre-push hook→ run before push -
post-push hook→ run after push -
post-checkout hook→ run after a branch is checked out -
post-merge hook→ run after a merge -
Local hooks → stored in
.git/hooks -
Global hooks → stored in
~/.git-templates/hooks
Create a New Hook
-
touch .git/hooks/pre-commit -
chmod +x .git/hooks/pre-commit- local
-
touch ~/.git-templates/hooks/pre-commit -
chmod +x ~/.git-templates/hooks/pre-commit -
Bash scripting
git checkout HEAD^→ go to previous commitgit diff→ see differencegit checkout <filename>→ undo the changegit restore→ restore filegit restore --source HEAD~N <file-name>git restore --staged <filename>git revert <hash>→ create new commit reverting the change
Move Commit from One Branch to Another
- Switch to branch
git switch mybranchgit log→ get commit hashgit switch mastergit cherry-pick <commit-hash>
Tag ≃ Bookmarks
-
Lightweight tag → name
git tag v1.0
-
Annotated tag → name + desc
git tag v1.0 -m
Git Tags / Rebase / Reflog
-
git tag→ list of the tag -
git push origin <tag name>→ push tag to remote -
git tag -d <tag name>→ delete -
git push origin --delete <tag> -
git diff main origin/main- comparer les différences entre local et remote
-
git rebase -i HEAD~N- combien de commit en arrière
- réécrire, fusionner, réorganiser
- corriger l'historique de plusieurs commits en une seule session
-
pick→ garder tel quel -
reword→ changer le message -
squash→ fusionner plusieurs commit -
edit→ modifier un commit -
drop→ supprimer un commit
-
git reflog→ voir l'historique pour revert -
git reset --hard HEAD@{?}- ou
git checkout <commit-id>
- ou
-
git merge --continue→ to follow git merge after diff
(97)