0% found this document useful (0 votes)
13 views

GIT GUIDE

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

GIT GUIDE

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Git Commands

Git Basics and Core Concepts


Git is a distributed version control system that helps developers track changes, collaborate
on projects, and manage source code effectively. Understanding its core commands is
crucial for efficient software development.

Detailed Command Breakdown


1. git init
• Purpose: Initialize a new Git repository

• Usage: Creates a new .git subdirectory in your current project folder

• Example:

git init my-project


cd my-project

• Key Points:

– Transforms an existing directory into a Git repository


– Creates the initial .git directory with repository metadata
– Does not affect existing files in the directory

2. git config
• Purpose: Set configuration values for your Git installation

• Types of Configuration:

– System-level: --system (applies to all users)


– User-level: --global (applies to current user)
– Project-level: --local (applies to current repository)
• Common Configuration Examples:

# Set user name


git config --global user.name "Your Name"

# Set email
git config --global user.email "your.email@example.com"

# List all configurations


git config --list

3. git add
• Purpose: Stage changes for commit
• Variants:

– git add <filename>: Stage specific file


– git add .: Stage all changes in current directory
– git add -A: Stage all changes across the entire repository
• Example:

git add README.md # Stage specific file


git add . # Stage all changes
git add src/*.js # Stage all JS files in src directory

4. git commit
• Purpose: Save staged changes to the repository

• Common Options:

– -m: Add commit message


– -a: Commit all modified files (skips staging)
– --amend: Modify the most recent commit
• Example:

git commit -m "Add login functionality"


git commit -am "Quick fix for authentication bug"

5. git clone
• Purpose: Create a copy of a remote repository

• Usage: Downloads entire project history and all branches

• Example:

git clone https://github.com/user/repository.git


git clone https://github.com/user/repository.git my-folder

6. git remote
• Purpose: Manage connections to remote repositories

• Common Commands:

# Add a new remote repository


git remote add origin https://github.com/user/repo.git

# List remote repositories


git remote -v

# Rename a remote
git remote rename old-name new-name

7. git push
• Purpose: Upload local repository changes to a remote repository
• Common Usage:

# Push to current branch


git push origin main

# Push and set upstream


git push -u origin feature-branch

# Force push (use with caution)


git push -f origin main

8. git pull
• Purpose: Fetch and merge changes from a remote repository

• Behavior: Combines git fetch and git merge

• Example:

git pull origin main


git pull --rebase origin main # Rebase instead of merge

9. git fetch
• Purpose: Download changes from remote without merging

• Usage:

git fetch origin


git fetch --all # Fetch from all remotes
git fetch origin branch-name # Fetch specific branch

10. git branch


• Purpose: Manage and create repository branches

• Commands:

# List branches
git branch

# Create new branch


git branch feature-login

# Delete a branch
git branch -d feature-login

# Rename current branch


git branch -m new-branch-name

11. git checkout


• Purpose: Switch branches or restore working tree files

• Usage:
# Switch to an existing branch
git checkout feature-branch

# Create and switch to new branch


git checkout -b new-feature

# Restore file to last committed state


git checkout -- filename

12. git merge


• Purpose: Combine changes from different branches

• Example:

# Merge feature branch into main


git checkout main
git merge feature-branch

# Merge with no fast-forward


git merge --no-ff feature-branch

Best Practices
• Always pull before pushing
• Write clear, descriptive commit messages
• Use branches for new features
• Avoid force pushing to shared branches
• Regularly communicate with team about branch changes

Workflow Recommendation
1. Create branch for new feature
2. Make changes and commit frequently
3. Push branch to remote repository
4. Create pull request
5. Review and merge changes
6. Delete feature branch after merging

You might also like