Managing Git Workflow: Merging and Pushing Code Safely
Scenario 1: Merge Code from develop
to main
and Keep develop
for Ongoing Work
To merge your finished work from the develop
branch into the main
branch while keeping the develop
branch intact for future development, follow these steps:
# Switch to the main branch:
git checkout main
# If you have any uncommitted changes, temporarily save them
# using the stash:
git stash
# Fetch the latest changes and rebase to ensure your local main branch
# is up-to-date:
git pull --rebase
# Merge develop into main:
git merge develop -m "merge message"
# Note: If there are conflicts during the merge, resolve them manually before proceeding.
# Push Changes to the Remote main Branch:
git push
# Switch Back to the develop Branch:
git checkout develop
# Reapply the previously stashed changes to the develop branch:
git stash pop
Scenario 2: Push Local Code to the Remote develop
Branch
Here’s a safe and structured approach to push local code to the remote develop
branch. This ensures that your code integrates smoothly with the existing branch and avoids conflicts:
# Check Your Current Branch to ensure you are on the develop branch:
git status
# Add the specific files to the staging area:
git add <file name 1> <file name 1>
# If you want to stage all changes, you can use:
git add .
# If you have any uncommitted changes, temporarily save them
# using the stash:
git stash
# Fetch the latest changes and rebase to ensure your local develop branch
# is up-to-date:
git pull --rebase
# push local changes to remote develop branch:
git push
# Reapply the previously stashed changes to the develop branch:
git stash pop
Why--rebase
?
Rebase avoids unnecessary merge commits, keeping the commit history clean.
Why This Approach Is Safe:
- Ensures your local branch is synchronized with the remote branch before pushing.
- Keeps the commit history clean using
rebase
. - Temporarily stashes un-staged changes to avoid accidental commits or conflicts.
These methods minimize the risk of introducing errors into the remote branch while maintaining a clean and organized workflow.
Do you enjoy this blog post?