Master Git: Essential Commands for Pushing, Branching, and Migration
This guide walks you through essential Git operations—including initializing a repository, pushing local projects to remote servers, reverting to previous commits, renaming and deleting branches, copying master code, migrating repositories while preserving history, and a collection of useful commands for efficient version control management.
1. Push local project to remote repository
git init # initialize repository
git remote -v # view associated remote URLs
git add . # stage all changes
git commit -m "First commit" # commit with message
git remote add origin <url> # add remote repository
git pull --rebase origin master # sync with remote
git push -u origin master # push to remote (use -f to force)2. Revert to a specific historical version in IDEA
# Find the revision number via IDEA: Right‑click project → Git → Show History → select version → Copy Revision Number
# In IDEA terminal:
git reset --hard <revision-id>
# Force‑push the reverted state:
git push -f -u origin master # or git push -f3. Modify the remote URL of a project
# Change remote URL via command
git remote set-url origin <new-url>
# Or edit the config file directly in .git/config and update the url entry4. Rename a Git branch
# Example: rename br_rename_old to br_rename_new
git checkout br_rename_old # switch to old branch (if not already there)
git pull origin br_rename_old # ensure up‑to‑date
git branch -m br_rename_old br_rename_new # rename locally
git push --set-upstream origin br_rename_new # push new branch
git push origin --delete br_rename_old # delete old remote branch5. Delete a Git branch
# Assume you are on a different branch, e.g., dev20180927
git checkout dev20180927
# Delete local branch
git branch -d dev20181018 # safe delete
# Force delete if necessary
git branch -D dev20181018
# Delete remote branch (use with caution)
git push origin --delete dev201810186. Copy master branch code to a new branch
git branch developer # create new branch
git checkout developer # switch to new branch
git merge master # merge master into new branch
git push origin developer # push new branch to remote7. Migrate a repository to another server while preserving branches and history
git clone --bare ssh://old-repo/project.git
cd project.git
git push --mirror ssh://new-repo/new-project.git8. Common Git command reference
# List all branches (local + remote)
git branch -a
# List local branches
git branch
# List remote branches
git branch -r
# Create a new local branch
git branch <branchName>
# Switch to a branch
git checkout <branchName>
# Push local branch to remote and set upstream
git push origin -u <branchName>
# Merge another branch into current
git merge <name>
# Clone a specific branch
git clone -b develop https://gitlab.xxxSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
