Operations 6 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Master Git: Essential Commands for Pushing, Branching, and Migration

1. Push local project to remote repository

<code>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)
</code>

2. Revert to a specific historical version in IDEA

<code># 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 -f
</code>

3. Modify the remote URL of a project

<code># 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 entry
</code>

4. Rename a Git branch

<code># 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 branch
</code>

5. Delete a Git branch

<code># 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 dev20181018
</code>

6. Copy master branch code to a new branch

<code>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 remote
</code>

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

<code>git clone --bare ssh://old-repo/project.git
cd project.git
git push --mirror ssh://new-repo/new-project.git
</code>

8. Common Git command reference

<code># 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.xxx
</code>
gitcommand lineVersion Controlbranchingrepository-migration
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login 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.