Publishing Your Project to GitHub in Minutes with Git

Git Push/Pull Workflow Guide

This reference covers the essential Git commands to syncing between local and remote repositories, particularly useful when working with both GitHub and Gitee.

Since repository switching is common, local user configuration isn't set globally—account info needs to be configured per repository.


Scenario 1: Pushing Local Project to Fresh Remote Repository

Case A: Remote Empty, No Local Repository

Initialize Local Repository

mkdir myproject
cd myproject
git init
git config user.name "YourName"
git config user.email "your.email@example.com"

Create and Stage Files

touch README.md
git add README.md
git commit -m "initial commit"

Link Remote and Push

git remote add origin https://github.com/username/myproject.git
git push -u origin "master"

If the remote origin already exists, remove it first:

git remote rm origin
git remote add origin https://github.com/username/myproject.git
git push -u origin "master"

Push Remaining Project Files

git add .
git commit -m "add project files"
git push -u origin "master"

Case B: Remote Contains README, Local Repository Exists

cd myproject
git pull --rebase origin master
git add .
git commit -m "sync and update"
git push -u origin "master"

Scenario 2: Clone Remote, Modify, and Push Back

Clone and Initialize

cd myproject
git init
git remote add origin http://repository-url.git

Pull Remote Content

git pull --rebase origin main

Configure and Modify

git config user.name "YourName"
git config user.email "your.email@example.com"
# Make your changes to project files

Push to Master Branch

git add .
git commit -m "update description"
git push origin master

Push to Different Branch

git branch feature-branch
git checkout feature-branch
git add .
git commit -m "feature update"
git push origin feature-branch

Scenario 3: Working with Feature Branches

Clone Specific Branch

git clone -b mybranch https://github.com/username/repo.git

Or clone master first, then create branch:

git clone https://github.com/username/repo.git
git branch mybranch

Switch and Work on Branch

git branch              # List local branches
git checkout mybranch   # Switch to your branch
git branch              # Confirm switch

Stage and Commmit Changes

git status              # Check modified files
git add file1 file2     # Stage specific files
git commit -m "change description"

Push Branch to Remote

git push origin mybranch  # Auto-creates branch if not exists

Delete Remote Branch

git push origin -d mybranch

Scenario 4: Sync Master to Feature Branch

git checkout master
git pull
git checkout mybranch
git merge master
git push origin mybranch

Common Errors and Solutions

Push Rejected: Remote Has Newer Changes

Error:

! [rejected] master -> master (fetch first)
error: failed to push some refs

Solution: Force push (use cautiously):

git push -f

Pull Failed: Unstaged Changes

Error:

error: Cannot pull with rebase: You have unstaged changes.

Solution:

git stash          # Save current changes
git pull --rebase  # Pull remote
git stash pop      # Restore saved changes

Key Differences

Command Behavior
git pull Fetch + merge
git pull --rebase Fetch + rebase

The rebase option creates a linear history by applying local commits on top of remote chenges.


Quick Reference

# Clone specific branch
git clone -b develop https://github.com/user/repo.git

# Common workflow
git add .
git commit -m "message"
git push origin branch-name

Tags: Git GitHub version-control Workflow

Posted on Mon, 12 Oct 2026 16:14:11 +0000 by CrimsonSoul