Operations 6 min read

Master Git: Push, Revert, Rename Branches, and Migrate Repositories Step‑by‑Step

This guide walks you through essential Git operations—including pushing a local project to a remote repository, reverting to previous commits, changing remote URLs, renaming and deleting branches, copying master to a new branch, and migrating repositories while preserving history.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Git: Push, Revert, Rename Branches, and Migrate Repositories Step‑by‑Step

Introduction

Tip: The following is the main content of the article; the examples below can be used as reference.

1. Push a local project to a remote repository

1、git init(initialize)
2、git remote -v (view associated remote URLs)
3、git add . (stage all changes)
4、git commit -m "first commit"(commit message)
5、git remote add origin xxx(add remote repository)
6、git pull --rebase origin master(sync local with remote)
7、git push -u origin master(push to remote)-f:force push

2. Revert to a specific historical version in IntelliJ IDEA

1. Find the revision number (right‑click project → Git → Show History → select version → Copy Revision Number)

2. Open IDEA terminal and run:

git reset --hard 139dcfaa558e3276b30b6b2e5cbbb9c00bbdca96  (replace with your revision number)

3. Push the changes to the remote server:

git push -f -u origin master  or  git push -f (force sync)

3. Change the remote URL of a project

1. Command method:
   git remote set-url origin <url>
2. Manual method:
   Edit the config file under the .git folder and modify the url entry.

4. Rename a Git branch

Assume you need to rename branch br_rename_old to br_rename_new:
1. git checkout br_rename_old   (switch to the old branch if not already there)
2. git pull origin br_rename_old   (sync with remote)
3. git branch -m br_rename_old br_rename_new   (rename locally)
4. git push --set-upstream origin br_rename_new   (push the new branch)
5. git push origin --delete br_rename_old   (delete the old remote branch)

5. Delete a Git branch

I am on dev20181018 and want to delete it:
1. Switch to another branch: git checkout dev20180927
2. Delete local branch: git branch -d dev20181018
3. If it fails, force delete: git branch -D dev20181018
4. Delete remote branch (use with caution): git push origin --delete dev20181018

6. Copy master branch code to a new branch

1. Create new branch: git branch developer
2. Switch to the new branch: git checkout developer
3. Merge master into it: git merge master
4. Push to remote: git push origin developer

7. Migrate a project to another repository while preserving branches and commit history

git clone --bare ssh://source-repo/project.git
cd project.git
git push --mirror ssh://target-repo/new-project.git

8. Common Git commands

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 branches:
git checkout <branchName>

Push a local branch to remote:
git push origin -u <branchName>

Merge branches:
git merge [name]   (merge branch [name] into current branch)

Clone a specific branch:
git clone -b develop https://gitlab.xxx
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevOpsGitcommand-lineVersion ControlBranch Managementrepository migration
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.