Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

GIT/shaare/kW0W5g

  • git
  • git

  • git init → initialize a new Git repo
  • git clone → clone remote repo
  • git add → stage change for next commit
  • git commit → create new commit with staged changes
  • git push → upload local commits to a remote repo
  • git pull → download change from remote to local
  • git checkout → switch between branches or restore
  • git merge → combine change from branches

Installing Git

  • git config --global
    • user.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 state
  • git 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
  • git add [tag]

  • git add .

  • git add *.txt


  • git reset [tag] → reset

.gitignore

  • Create a .gitignore in 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 code
  • develop → dev code with new features

Supporting Branches

  • feature → dev new feature
  • release → use to prepare a new release
  • hotfix → quick fix of critical bug
  1. Start new feature

    • branch from develop
  2. Work on new branch & commit

  3. Merge feature back to develop

  4. Create a release

    • branch off develop
    • release/1.2.0
  5. Finalize release

    • make change
    • merge release into master
    • develop
  6. Fix a bug

    • branch off master
    • hotfix/1.2.1
    • merge

Pull Request Workflow

  1. Branch out → create new branch from master
  2. Commit change → commit your work to new branch
  3. Push the branch → push branch to the remote
  4. Open a pull request → go to web interface, create pull request
  5. Add a description → describe change you've made
  6. 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

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 commit
  • git diff → see difference
  • git checkout <filename> → undo the change
  • git restore → restore file
  • git 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 mybranch
    • git log → get commit hash
    • git switch master
    • git 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>
  • git merge --continue → to follow git merge after diff

2 months ago Permalink
cluster icon
  • No related link


(97)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community