Skip to content

Latest commit

 

History

History
117 lines (87 loc) · 2.87 KB

Advance Git Operations.md

File metadata and controls

117 lines (87 loc) · 2.87 KB

Advanced Git Operations and Best Practices:

1. Rebasing:

  • Interactive Rebasing:

    git rebase -i HEAD~n
    • Allows you to squash, edit, reorder, or drop commits interactively. 🔄
  • Rebase with Upstream Changes:

    git pull --rebase origin main
    • Rebases your local changes on top of the latest changes from the upstream branch. 🌐

2. Cherry-Picking:

  • Cherry-Pick a Commit:
    git cherry-pick <commit-hash>
    • Applies the changes of a specific commit onto your current branch. 🍒

3. Stashing:

  • Stash Changes:

    git stash save "your stash message"
    • Temporarily saves your changes without committing them. 📦
  • Apply Stash:

    git stash apply stash@{n}
    • Applies a specific stash to your working directory. 🧑‍🎨

4. Reflog:

  • View Reflog:

    git reflog
    • Shows a log of all the branch references. 📜
  • Undoing Changes with Reflog:

    git reset --hard HEAD@{n}
    • Resets the repository to a previous state using the reflog. ⏪

5. Submodules:

  • Add a Submodule:

    git submodule add <repository-url> path/to/submodule
    • Adds a submodule to your project. ➕
  • Update Submodules:

    git submodule update --recursive --remote
    • Updates submodules to the latest commit. 🔄

6. Git Hooks:

  • Client-Side Hooks:

    • Pre-commit, post-commit, pre-push, etc.
    • Located in the .git/hooks/ directory. 🎣
  • Server-Side Hooks:

    • Pre-receive, post-receive, etc.
    • Located in the server's repository hooks directory. 🖥️

7. Git Worktree:

  • Create a Worktree:
    git worktree add -b new-branch path/to/new-worktree
    • Creates a new working directory and branch to work on simultaneously. 🏗️

Best Practices:

  1. Commit Small, Logical Units:

    • Make commits focused on a single task. 🎯
  2. Write Descriptive Commit Messages:

    • Clearly communicate the purpose of your changes. 📝
  3. Use Branches Effectively:

    • Create feature branches for new development.
    • Use topic branches for bug fixes. 🌿
  4. Regularly Fetch and Pull:

    • Keep your local repository up to date. 🔄
  5. Keep Repositories Clean:

    • Remove obsolete branches.
    • Use .gitignore to exclude unnecessary files. 🧹
  6. Use Git Tags for Releases:

    • Tag important commit points for releases. 🏷️
  7. Document Your Workflow:

    • Provide a README with instructions for setting up and running the project. 📚
  8. Collaborate and Communicate:

    • Discuss changes with your team before major modifications. 💬
  9. Review and Test Before Pushing:

    • Avoid pushing code that breaks the build. 🚨
  10. Backup Important Branches:

    • Regularly back up important branches. 💾