Keywords: Git Branch Management | GitHub Collaboration | Development Workflow
Abstract: This article provides a comprehensive guide on correctly creating a development branch from the master branch in GitHub repositories. It analyzes common mistakes in git push operations, explains the mapping between local and remote branches, and presents complete workflows for branch creation, pushing, management, and deletion. The guide covers both command-line operations and GitHub's graphical interface to help teams establish standardized branch management strategies.
Problem Context and Common Mistakes Analysis
In team collaboration development, working directly on the master branch poses significant risks. A better approach involves creating a development branch as the primary development branch, where team members commit code and submit pull requests for code review before merging into master. However, many developers encounter confusion when creating remote branches.
A typical incorrect operation is as follows:
git checkout -b development
git push origin development:master
The issue with this operation lies in misunderstanding the git push command. git push origin development:master means pushing the local development branch to the remote master branch, not creating a new remote development branch. Since the local development branch is created from master and contains identical commit history, Git reports "Everything up-to-date" without actually creating a new remote branch.
Correct Branch Creation Method
To properly create a remote development branch, use the following command sequence:
# Create local development branch from current branch (typically master)
git checkout -b development
# Push local development branch to remote repository, creating corresponding remote branch
git push origin development
This operation works correctly because git push origin development creates a new branch named development in the remote repository with content identical to the local development branch. Since the local development branch is derived from master, the remote development branch naturally contains all content from the master branch.
GitHub Graphical Interface Branch Creation
In addition to command-line methods, GitHub provides an intuitive graphical interface for branch creation:
Creating via branches overview page:
- Navigate to the main page of your GitHub repository
- From the file tree view on the left, select the branch dropdown menu, then click "View all branches"
- Click the "New branch" button
- Under "Branch name", type development
- Under "Branch source", choose master as the source branch
- Click "Create branch" to complete the operation
Creating directly via branch dropdown:
- Select the branch dropdown menu on the repository page
- In the "Find or create a branch..." text field, type development
- Click "Create branch"
Branch Management Best Practices
After successfully creating the development branch, teams should follow this workflow:
First push with upstream setup:
git push -u origin development
The -u parameter (equivalent to --set-upstream) establishes tracking between the local development branch and the remote development branch. Once set, subsequent git push commands automatically push to the correct remote branch.
Daily development workflow:
# Switch to development branch
git checkout development
# Make code changes
# Add changes to staging area
git add .
# Commit changes
git commit -m "Feature description"
# Push to remote development branch
git push
Branch Merging and Deletion
When development branch features are complete and tested, merge into master branch:
# Switch to master branch
git checkout master
# Pull latest master branch content
git pull origin master
# Merge development branch into current branch (master)
git merge development
# Push updates to remote master branch
git push origin master
If the development branch is no longer needed, it can be safely deleted:
# Delete remote development branch
git push -d origin development
# Delete local development branch
git branch -d development
Note that if the branch to be deleted is the repository's default branch, a new default branch must be selected first. If the branch is associated with open pull requests, those pull requests must be merged or closed first.
Conclusion
The key to correctly creating GitHub remote branches lies in understanding the semantics of the git push command. Using git push origin branch_name directly creates a corresponding branch in the remote repository. Combined with GitHub's graphical interface tools, teams can more flexibly manage branch strategies and establish standardized code review and merge processes to ensure code quality and project stability.