DEV Community

KAMAL KISHOR
KAMAL KISHOR

Posted on

Master Git with Multiple Remotes: A Complete Developer Guide

Table of Contents

  1. What is Git?
  2. Basic Git Setup
  3. Core Git Commands
  4. Branching in Git
  5. Working with Remotes
  6. Handling Merge Conflicts
  7. Git Log & History
  8. Stashing Changes
  9. Rebasing & Reset
  10. Working with Tags
  11. Git Ignore & Clean
  12. Managing Multiple GitHub Accounts
  13. Automation with Git Scripts
  14. Markdown Cheatsheet
  15. Git Pro Tips

What is Git?

Git is a distributed version control system that allows developers to track changes in source code over time. It enables collaborative work by allowing multiple developers to work on the same project without interfering with each other’s progress.


Basic Git Setup

Before using Git, you should configure your Git environment. Here's how:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
Enter fullscreen mode Exit fullscreen mode
  • git config --global user.name: Set your name.
  • git config --global user.email: Set your email address.
  • git config --global init.defaultBranch main: Set default branch name as main.
  • git config --global core.editor "code --wait": Set default editor to Visual Studio Code.

Core Git Commands

Here are some essential Git commands for everyday use:

git init                 # Initialize a new Git repository
git clone <url>          # Clone a repository
git status               # Show the current status of your working directory
git add .                # Stage all changes
git commit -m "Message"  # Commit changes
git push                 # Push to remote repository
git pull                 # Pull from remote repository
git fetch                # Fetch changes from the remote
git log                  # Show commit history
Enter fullscreen mode Exit fullscreen mode

Branching in Git

Branching allows you to work on separate features without interfering with the main project. Here are some basic branch commands:

git branch                   # List all branches
git branch <name>            # Create a new branch
git checkout <name>          # Switch to a branch
git checkout -b <name>       # Create and switch to a new branch
git merge <branch>           # Merge another branch into the current branch
Enter fullscreen mode Exit fullscreen mode

Working with Remotes

Git allows you to manage different versions of your project across multiple locations (remotes). Here's how to work with remotes:

git remote -v                           # List the remote repositories
git remote add origin <url>             # Add a remote repository
git push origin main                    # Push changes to the origin remote
git push --set-upstream origin feature  # Track a branch with remote
Enter fullscreen mode Exit fullscreen mode

Handling Merge Conflicts

Merge conflicts happen when two developers make changes to the same part of a file. To resolve:

  1. Pull the latest changes:
   git pull
Enter fullscreen mode Exit fullscreen mode
  1. Git will mark conflicts in the files. Edit the conflicted files, then:
   git add .
   git commit -m "Resolved merge conflicts"
Enter fullscreen mode Exit fullscreen mode

Git Log & History

Use git log to view your commit history. You can view detailed logs and make them easier to read:

git log                 # Show commit history
git log --oneline       # Show commits as one line per commit
git log --graph --all   # Visualize the commit history with a graph
Enter fullscreen mode Exit fullscreen mode

Stashing Changes

If you need to switch branches but aren’t ready to commit your changes, use git stash:

git stash              # Save uncommitted changes
git stash pop          # Apply the last stashed changes
git stash list         # View stashed changes
git stash drop 0       # Drop the first stash
Enter fullscreen mode Exit fullscreen mode

Rebasing & Reset

Rebase and reset commands help you modify your commit history:

git rebase main              # Rebase your current branch onto the main branch
git reset --hard HEAD~1      # Remove the last commit (hard reset)
git reset --soft HEAD~1      # Remove the last commit but keep changes (soft reset)
Enter fullscreen mode Exit fullscreen mode

Working with Tags

Tags are useful for marking specific points in your project’s history:

git tag v1.0                 # Create a tag
git tag                     # List all tags
git push origin v1.0        # Push a tag to the remote repository
git push origin --tags      # Push all tags
Enter fullscreen mode Exit fullscreen mode

Git Ignore & Clean

To avoid tracking unnecessary files, use .gitignore:

Example .gitignore:

node_modules/
.env
*.log
Enter fullscreen mode Exit fullscreen mode

To clean up untracked files:

git clean -fd
Enter fullscreen mode Exit fullscreen mode

Managing Multiple GitHub Accounts

1. Generate SSH Keys

Generate unique SSH keys for different accounts:

ssh-keygen -t rsa -C "email1@example.com" -f ~/.ssh/id_rsa_personal
ssh-keygen -t rsa -C "email2@example.com" -f ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

2. Edit ~/.ssh/config

Configure SSH to manage multiple accounts:

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal

Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_work
Enter fullscreen mode Exit fullscreen mode

3. Add Remotes for Different Accounts

git remote add personal git@github.com-personal:your/repo.git
git remote add work git@github.com-work:your/repo.git
Enter fullscreen mode Exit fullscreen mode

4. Push to Either or Both

You can now push to different remotes:

git push personal main
git push work main
Enter fullscreen mode Exit fullscreen mode

Create an alias to push to both:

git config alias.pushall '!git push personal && git push work'
git pushall
Enter fullscreen mode Exit fullscreen mode

Automation with Git Scripts

You can automate common tasks with shell scripts. Here’s an example script to push to all remotes:

#!/bin/bash
branches=$(git branch --show-current)
git push personal $branches
git push work $branches
git push origin $branches
Enter fullscreen mode Exit fullscreen mode

Make it executable:

chmod +x git-multi-push.sh
./git-multi-push.sh
Enter fullscreen mode Exit fullscreen mode

Markdown Cheatsheet

Here’s a quick reference for Markdown formatting:

# Heading 1
## Heading 2
**Bold** *Italic* `inline`
- List item
[Link](https://example.com)
Enter fullscreen mode Exit fullscreen mode

Git Pro Tips

Here are some Git pro tips for optimizing your workflow:

  1. Use git reflog to recover lost commits.
  2. Use git bisect for debugging bad commits.
  3. Configure .gitattributes for handling cross-platform line endings.
  4. Set up pre-commit hooks to automate tasks like linting.

Final Thoughts

By mastering Git, you can significantly improve your workflow and collaboration with other developers. This tutorial covers everything from basic Git commands to advanced configuration and automation, helping you become more efficient with version control.

Happy coding!

Top comments (0)