Navigating the world of Git can be akin to mastering an arcane art. For the savvy developer, knowing the right incantations can mean the difference between seamless collaboration and catastrophic code conflicts. Here's a compendium of Git commands that are essential for those critical moments. Handle them with care, for they wield great power.
Reverting to a Specific Commit (Non-Shared Branches Only)
When you need to undo changes and return to a known good state:
- `git reset --hard <commit-hash>` - This will reset your branch to the specified commit.
- `git push -f origin` - Force push the changes to overwrite the remote branch.
Merging Branches Like a Pro
To incorporate changes from one branch into another, follow these steps:
- `git checkout <branch-to-merge-to>` - Switch to the branch you want to update.
- `git fetch origin` - Fetch the latest changes from the remote.
- `git pull` - Pull the latest changes into your local branch.
- `git status` - Check the status of your branch.
- `git pull origin <branch-with-new-changes>` - Pull in changes from another branch.
Selective File Pulling
Because sometimes you only need one file from another branch:
- `git checkout origin/<branch-name> <file-relative-path>` - This command will fetch that specific file.
Pruning Like a Gardener
Keep your local and remote branches in sync:
- `git config remote.origin.prune true` - This will prune branches that no longer exist on the remote.
Tidying Up Local Branches
Delete a local branch that's no longer needed:
- `git checkout <other-branch>` - Switch to a different branch.
- `git branch --delete <branch-to-delete>` - Delete the specified branch.
- `git branch -a` - List all branches.
- `q` - Exit the list view.
Cherry-Picking with Precision
When you want to apply specific commits from one branch to another:
- `git cherry-pick <commit-hash>` - This command allows you to select individual commits.
Unsticking Auto-deploy with an Empty Commit
If your auto-deploy webhook process is stuck, this might just be the nudge it needs:
- `git commit --allow-empty -m “chore:empty commit”` - Create an empty commit.
Collaborating on Another Developer's Branch
To check out a branch from another developer:
- `git checkout main` - Return to the main branch.
- `git fetch origin` - Fetch the latest updates.
- `git pull` - Pull in any new changes.
- `git checkout origin/<other-dev-branch>` - Switch to the other developer's branch.
Branching Out from an Old Remote Branch
- To create a new local branch based on an old one:
- `git fetch origin` - Update your list of remote branches.
- `git checkout -b <new-branch-name> origin/<old-branch-name>` - Create and switch to your new branch.
- `git branch -a` - Verify by listing all branches.
Comments
Post a Comment