Standard Synchronization and Commit Workflow
Always synchronize your local repository with the remote before pushing new changes to prevent integration conflicts.
git fetch origin
git pull origin mainThe recommended sequence for persisting local modifications is:
git add .
git commit -m "Implement dashboard feature"
git pull origin main
git push origin mainAvoid bypassing pre-commit hooks using --no-verify unless absolutely necessary, as these hooks enforce code quality and catch potential defects early.
If you need to synchronize remote updates but are not ready to push, execute git pull. In the event of merge conflicts, temporarily revert local changes on the affected files, pull the updates, and then reapply your modifications.
Temporarily Switching Contexts
To switch branches while retaining uncommitted work, stash your changes to clear the working tree, retrieve external updates, and then restore your stashed state.
git stash push -m "WIP: dashboard styling"
git stash popNote that restoring a stash might overwrite newly pulled updates in files you had modified locally.
Integrating Specific Files from Another Branch
To overlay a specific directory or file from a different branch into your current working tree without a full merge:
git restore --source=feature/auth src/utils/helpers.tsFeature Branch Integration
When merging an isolated feature into the primary development line:
git push origin feature/dashboard
git switch main
git pull origin main
git merge feature/dashboard
git push origin mainBranch Creation and Remote Tracking
Prior to creating or switching branches, ensure local metadata reflects the remote state:
git fetch --allSwitching to an Existing Branch
git switch feature/auth
git pull origin feature/authCreating a New Local Branch
If the remote lacks the branch, generate and push it:
git switch -c feature/auth
git push -u origin feature/authThe -u flag establishes an upstream tracking relationship, simplifying future pushes and pulls.
Linking to an Existing Remote Branch
git switch -c feature/auth origin/feature/authExtracting Commits Across Branches
To apply specific commits from a target branch into your current context, identify the commit hash via git log and apply it:
git cherry-pick a1b2c3dBranch Inventory
git branch # Local branches
git branch -r # Remote references
git branch -a # All branchesBranch Deletion
Ensure you are operating from a separate branch before removing a target branch.
git branch -D feature/legacy
git push origin --delete feature/legacyResolving Common Push Failures
Interrupted Rebase
If a rebase operation halts prematurely, leaving a locked state, you can either forcefully clear the rebase state or resolve the conflicts and proceed:
rm -rf .git/rebase-merge
# OR
git rebase --continueRemote Divergence
When the local history is out of sync with the remote, a pull is required before pushing. Using rebase during the pull can maintain a cleaner commit history:
git pull --rebase origin main
git push origin mainBranch Name Mismatch
Push rejections due to incorrect branch names are common. Always verify the active branch using git branch and copy the exact name to avoid typographical errors.