Most Commonly Used Git Commands (With Examples)

Git is one of the most powerful version control systems used in software development. Whether you are working solo or as part of a team, knowing the most commonly used Git commands helps you manage code, collaborate efficiently, and resolve issues quickly.

In this guide, let’s look at some everyday Git commands with simple examples and explanations.


🧩 1. Merge One Branch With Another

Use this command to merge another branch into your current branch.

git merge <branch-name>

Example:

git merge develop

👉 This merges the develop branch into your current branch (say feature/login).


🔗 2. Link a Local Branch With a Remote Branch

When you want to connect your local branch to a branch in the remote repository (like origin), use:

git branch --set-upstream-to=origin/<remote-branch-name> <local-branch-name>

Example:

git branch --set-upstream-to=origin/develop feature/develop

This links your local feature/develop branch to the remote origin/develop.


🧹 3. Discard Local Commits and Sync With Remote Branch

If you’ve made commits locally that haven’t been pushed yet — and you want to discard them to match the remote branch — use:

git reset --hard origin/<remote-branch-name>

Example:

git reset --hard origin/main

⚠️ Warning: This permanently removes unpushed commits from your local branch.


🔄 4. Merge (Pull) Remote Branch Into Local Branch

To pull and merge changes from a remote branch (e.g., develop) into your current branch:

git pull origin <branch-name>

Example:

git pull origin develop

This fetches the latest changes from the develop branch on the remote and merges them into your current branch.


🌍 5. Map Local Repository to an Existing Remote Repository

If you’ve created a local repo and want to connect it to a remote (e.g., GitHub), run:

git remote add origin <remote-repo-url>

Example:

git remote add origin https://github.com/Write4Dinesh/SampleAppsRepo.git

This tells Git that origin refers to that remote repository URL.


💬 6. Handling the VIM Editor During Merge or Pull

When merging or pulling, Git may open the VIM editor to enter a commit message. If you’re unfamiliar with VIM, follow these steps to close it:

  1. Press Ctrl + C (or Esc) to enter command mode
  2. Type :wq
  3. Press Enter

✅ This saves and exits VIM, completing the merge or pull process.


🗑️ 7. Delete a Branch Locally

To delete a branch from your local repository:

git branch -d <branch-name>

Example:

git branch -d feature/old-login

If Git refuses to delete because the branch isn’t merged, force-delete it using:

git branch -D <branch-name>

🔥 8. Delete a Branch From Both Local and Remote

To delete a branch in the remote repository and locally:

# Delete from remote
git push origin --delete <branch-name>

# Delete locally
git branch -d <branch-name>

Example:

git push origin --delete feature/old-login
git branch -d feature/old-login

🧠 Summary

ActionCommand
Merge another branchgit merge <branch>
Set upstream branchgit branch --set-upstream-to=origin/<remote> <local>
Reset to remotegit reset --hard origin/<branch>
Pull from remotegit pull origin <branch>
Add remote repogit remote add origin <url>
Delete local branchgit branch -d <branch>
Delete remote branchgit push origin --delete <branch>

🏁 Final Thoughts

Mastering these Git commands helps you stay in control of your codebase, handle version conflicts with confidence, and collaborate smoothly in large projects.

💡 Pro Tip: Before using git reset --hard, always make sure you’ve committed or stashed any important work to avoid losing changes permanently.


Tags: Git, Version Control, Developer Tools, GitHub, Cheat Sheet, DevOps

Meta Description (SEO):
A quick reference guide for the most commonly used Git commands with examples — merging branches, setting upstream, resetting, deleting branches, and managing remotes.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top