Comprehensive Guide to Git Commands: Mastering Version Control

Comprehensive Guide to Git Commands: Mastering Version Control

Git is a powerful version control system that allows developers to track changes, collaborate on code, and manage projects efficiently. Whether you're new to Git or need a quick refresher, this guide outlines the essential Git commands every developer sho

insight contents
insight contents 19 Nov 2024

1. Getting Started with Git

Installation and Setup

To install Git:

  • Windows: Download from Git for Windows.
  • Mac: Use Homebrew: brew install git.
  • Linux: Install via the package manager: sudo apt install git (Debian-based).

After installation, verify it with:

git --version

Basic Configuration

Set your username and email (essential for commit history):

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

To view configuration settings:

git config --list

2. Working with Repositories

Creating and Cloning Repositories

  • Initialize a new Git repository:

git init
  • Clone an existing repository:
git clone <repository-url>

Staging and Committing Changes

  • Check the current status of your repository:

git status
  • Add files to the staging area:
git add <file>
# Add all files
git add .
  • Commit changes to the repository:
git commit -m "Commit message"
  • View the changes made:
git diff

3. Branch Management

Creating, Switching, and Merging Branches

  • Create a new branch:

git branch <branch-name>
  • Switch to a branch:
git checkout <branch-name>
  • Merge a branch into the current branch:
git merge <branch-name>

Resolving Merge Conflicts

Conflicts occur when changes from different branches overlap. To resolve:

  1. Open conflicting files and resolve manually.
  2. Mark resolved files:
git add <file>

     3. Complete the merge:

git commit

4. Collaboration

Fetch, Pull, and Push

  • Fetch updates from the remote repository:

git fetch
  • Pull updates and merge them:
git pull
  • Push local commits to the remote repository:
git push

Working with Remote Repositories

  • Add a remote repository:

git remote add origin <repository-url>
  • List remote repositories:
git remote -v
  • Remove a remote:
git remote remove <name>

5. Advanced Commands

Reset, Revert, and Rebase

  • Undo changes (soft, mixed, or hard):

git reset --soft <commit-hash>
git reset --hard <commit-hash>
  • Revert a specific commit:
git revert <commit-hash>
  • Rebase your commits for a cleaner history:
git rebase <branch-name>

Stashing and Cleaning

  • Save uncommitted changes temporarily:

git stash
  • Apply stashed changes:
git stash apply
  • Remove untracked files:
git clean -f

6. Viewing History

  • View the commit history:

git log
  • View a concise history:
git log --oneline
  • Check changes for a specific file:
git log -p <file>

7. Tips and Tricks

Custom Aliases

Simplify commands with aliases:

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit

Ignoring Files

Create a .gitignore file to exclude files or directories:

/.phpunit.cache
/node_modules
/public/build
/public/hot
/public/storage
/storage/*.key
/vendor
!/vendor/firefly
.env
.env.backup
.env.production
.phpactor.json
.phpunit.result.cache
Homestead.json
Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
/.fleet
/.idea
/.vscode
  • Add it to the repository:
git add .gitignore

Conclusion

Mastering Git commands can significantly boost your productivity in version control. Start with the basics, and gradually incorporate advanced features into your workflow. Bookmark this guide for quick reference as you navigate through your Git journey.

Happy coding! 🚀💻💻

Leave a reply