Lädt...

🔧 The Complete Git Commands Cheat Sheet - Everything You Need to Know


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Git is the free and open source distributed version control system that's responsible for everything GitHub-related that happens locally on your computer. This comprehensive cheat sheet features the most important and commonly used Git commands for easy reference.

1. Getting Started & Configuration 🏁

Installation & GUIs

With platform specific installers for Git, GitHub also provides the ease of staying up-to-date with the latest releases of the command line tool while providing a graphical user interface for day-to-day interaction, review, and repository synchronization.

Installation Setup

Configuring user information used across all local repositories:

# Check Git version
git --version

# Set username
git config --global user.name "[name]"

# Set email
git config --global user.email "[email]"

# List all configurations
git config --list

# Enable helpful colorization
git config --global color.ui auto

# Display help documentation
git help

Repository Initialization

# Initialize a new Git repository
git init

# Create repository in specific directory
git init [directory]

# Clone a repository
git clone [url]

# Clone into specific directory
git clone [url] [directory]

# Clone specific branch
git clone --branch [branch_name] [repository_url]

2. Basic Snapshotting 📸

Adding Files

# Add file to staging
git add [file]

# Add all new and changed files
git add .

# Add all files (including deleted)
git add -A

# Interactive staging
git add -p

Committing Changes

# Commit with message
git commit -m "[descriptive message]"

# Add and commit tracked files
git commit -am "[message]"

# Modify most recent commit
git commit --amend

# Amend without changing message
git commit --amend --no-edit

# Create new note
git notes add

# Restore file to last commit
git restore <file>

Status & Differences

# Show modified files
git status

# Show ignored files
git status --ignored

# Show working directory changes
git diff

# Show staged changes
git diff --staged
git diff --cached

# Compare branches
git diff [branch1] [branch2]

# Unstage file
git reset [file]

# Compare with last commit
git diff HEAD

3. Branching and Merging 🌲

Branch Management

# List local branches
git branch

# List all branches
git branch -a

# Create new branch
git branch [branch-name]

# Delete branch locally
git branch -d [branch-name]

# Force delete branch
git branch -D [branch-name]

# Rename branch
git branch -m [old-name] [new-name]

# Switch to branch
git switch -c [branch-name]

Navigation

# Switch branches
git checkout [branch-name]

# Create and switch
git checkout -b [branch-name]

# Switch to last branch
git checkout -

# Check out commit
git checkout [commit-hash]

Merging

# Merge branch
git merge [branch]

# Abort merge
git merge --abort

# Create merge commit
git merge --no-ff [branch]

4. Remote Operations 🌐

Remote Management

# List remote repositories
git remote -v

# Add remote
git remote add [name] [url]

# Remove remote
git remote remove [name]

# Delete file
git rm [file]

# Rename remote
git remote rename [old-name] [new-name]

Syncing

# Fetch branches
git fetch [remote]

# Remove obsolete branches
git fetch --prune

# Fetch and merge
git pull

# Fetch and rebase
git pull --rebase

# Push changes
git push [branch]

# Push and set upstream
git push -u origin [branch]

# Force push
git push --force

5. History and Comparison 📚

Logging

# Show commit history
git log

# One line format
git log --oneline

# Show as graph
git log --graph

# Show with stats
git log --stat -M

# Show file changes by author
git blame [file]

# Compare branch commits
git log branchB..branchA

# Show file history
git log --follow [file]

Inspection

# Show commit details
git show [commit]

# Show current commit hash
git rev-parse HEAD

# Show reference logs
git reflog

# Show object details
git show [SHA]

6. Undoing Changes ↩️

Working Directory

# Discard changes
git checkout -- [file]

# Preview cleanup
git clean -n

# Remove untracked files
git clean -f

# Remove untracked directories
git clean -fd

Staged Changes

# Unstage file
git reset [file]

# Unstage all
git reset

# Reset to commit
git reset --hard [commit]

Commits

# Undo commits
git reset [commit]

# Create undo commit
git revert [commit]

# Revert without commit
git revert --no-commit <commit>

# Soft reset
git reset --soft [commit]

# Hard reset
git reset --hard [commit]

7. Ignoring Patterns 🚫

# Common .gitignore patterns
logs/
*.notes
pattern*/

# Set global ignore file
git config --global core.excludesfile [file]

8. Advanced Operations 🔧

Stashing

# Save changes
git stash

# List stashes
git stash list

# Apply top stash
git stash pop

# Apply without removing
git stash apply

# Remove top stash
git stash drop

# Clear all stashes
git stash clear

Rebasing

# Rebase onto branch
git rebase [branch]

# Interactive rebase
git rebase -i HEAD~[n]

# Abort rebase
git rebase --abort

# Continue rebase
git rebase --continue

# Move file
git mv [existing-path] [new-path]

Tags

# List tags
git tag

# Create lightweight tag
git tag [tag-name]

# Create annotated tag
git tag -a [tag-name] -m "[message]"

# Push tag
git push origin [tag-name]

9. Maintenance and Data Recovery 🛠️

Maintenance

# Check integrity
git fsck

# Clean up repository
git gc

# Remove unreachable objects
git prune

# Verify packed objects
git verify-pack -v .git/objects/pack/pack-*.idx

Recovery

# Expire reflog
git reflog expire --expire=now --all

# Recover commit
git checkout [lost-commit-hash]

# Cherry-pick commit
git cherry-pick [commit]

10. GitHub Specific Commands 🐙

Pull Requests

# Fetch PR
git fetch origin pull/[PR-number]/head:[branch-name]

# Check out PR
git checkout pr/[PR-number]

# Push to PR
git push origin [branch]:[PR-branch]

GitHub CLI

# Create repository
gh repo create

# Create pull request
gh pr create

# Create issue
gh issue create

# Fork repository
gh repo fork

11. Configuration and Info ⚙️

Help and Information

# Get command help
git help [command]

# Alternative help
git [command] --help

# Quick help
git -h

Advanced Configuration

# Set default editor
git config --global core.editor "[editor]"

# Create alias
git config --global alias.[shortcut] "[command]"

# Configure line endings
git config --global core.autocrlf true

# Cache credentials
git config --global credential.helper cache

# Set default branch
git config --global init.defaultBranch main

📖 Glossary

  • Git: An open-source, distributed version control system that tracks changes in code.
  • GitHub: A web-based platform for hosting, sharing, and collaborating on Git repositories.
  • Commit: A snapshot of the entire repository at a given point in time, identified by a unique SHA (hash).
  • Branch: A movable pointer to a commit, allowing parallel development without affecting the main codebase.
  • Clone: A local copy of a Git repository, including all commits, branches, and history.
  • Remote: A shared repository (typically on GitHub) that team members use to push and pull changes.
  • Fork: A personal copy of someone else's repository on GitHub, allowing independent modifications.
  • Pull Request (PR): A request to merge changes from one branch into another, often reviewed with comments, tests, and approvals.
  • HEAD: The current reference point in a Git repository, typically pointing to the latest commit on the active branch.

📚 Resources for Further Learning

Official Resources

Educational Platforms

Additional References

🤝 Contributing

Found this helpful?
Consider sharing it with your network!
If you spot any errors or have suggestions for improvement, feel free to leave a comment below.

Happy coding! 🚀

...

🔧 Git Cheat Sheet – Git Commands You Should Know


📈 59.35 Punkte
🔧 Programmierung

🔧 Git Commands You Need for Hacktoberfest 2024 - Git cheat sheet


📈 59.32 Punkte
🔧 Programmierung

🐧 Unix Commands Cheat Sheet: All the Commands You Need


📈 53.17 Punkte
🐧 Linux Tipps

🎥 Top Git Commands | Most Used Git Commands | Git Commands With Examples


📈 52.9 Punkte
🎥 Video | Youtube

🔧 Git Cheat Sheet: A Beginner's Guide to Git Commands and Best Practices


📈 49.86 Punkte
🔧 Programmierung

🔧 Git Cheat Sheet – Helpful Git Commands with Examples


📈 49.86 Punkte
🔧 Programmierung

🔧 Git Cheat Sheet - 40+ Git Comands You Should Know


📈 49.64 Punkte
🔧 Programmierung

🐧 Linux Command Line Cheat Sheet: All the Commands You Need


📈 43.46 Punkte
🐧 Linux Tipps

📰 Windows PowerShell Commands Cheat Sheet – The Ultimate Guide You Need


📈 43.46 Punkte
🖥️ Betriebssysteme

🔧 Complete GitHub Commands Cheat Sheet


📈 42.41 Punkte
🔧 Programmierung

🔧 🚀 Git Cheat Sheet – Copy &amp; Paste Commands Easily


📈 41.93 Punkte
🔧 Programmierung

🔧 for when i forget how to use GIT Quick Setup &amp; Commands Cheat Sheet (Linux, Mac &amp; Windows)


📈 41.93 Punkte
🔧 Programmierung

🔧 💡📖 Essential Git Commands: Your Go-To Cheat Sheet for Efficient Version Control


📈 41.93 Punkte
🔧 Programmierung

🔧 Git Cheat Sheet: Essential Commands for Version Control


📈 41.93 Punkte
🔧 Programmierung

🔧 Git Commands Cheat Sheet


📈 41.93 Punkte
🔧 Programmierung

🔧 GIT Commands Cheat Sheet


📈 41.93 Punkte
🔧 Programmierung

🔧 Git Commands - Cheat Sheet for All Developers


📈 41.93 Punkte
🔧 Programmierung

🐧 Git Commands Cheat Sheet


📈 41.93 Punkte
🐧 Server

🔧 Complete Git Cheat sheet


📈 40.63 Punkte
🔧 Programmierung

🔧 Kubernetes Cheat Sheet: Must-Know Commands and Examples


📈 40.1 Punkte
🔧 Programmierung

🔧 Docker Cheat Sheet all You Need To Know About Docker Command


📈 39.85 Punkte
🔧 Programmierung

🔧 Git Commands you Should Know if you Plan to Work with Git.


📈 38.46 Punkte
🔧 Programmierung

🔧 The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh


📈 35.78 Punkte
🔧 Programmierung

🔧 The Ultimate Cheat Sheet: CLI Man Pages, tldr, and cheat.sh


📈 35.78 Punkte
🔧 Programmierung

🔧 Batch Script Commands Cheat Sheet :)


📈 34 Punkte
🔧 Programmierung

🐧 Some Linux Commands &quot;Cheat-sheet&quot;


📈 34 Punkte
🐧 Linux Tipps

🔧 Windows Commands Cheat Sheet :)


📈 34 Punkte
🔧 Programmierung

🔧 Gradle Commands Cheat Sheet


📈 34 Punkte
🔧 Programmierung

🔧 Maven Commands Cheat Sheet


📈 34 Punkte
🔧 Programmierung

🔧 Cheat sheet of 'Alembic' commands


📈 34 Punkte
🔧 Programmierung

🔧 Tailwind Commands Cheat Sheet


📈 34 Punkte
🔧 Programmierung

🔧 Docker Commands Cheat Sheet


📈 34 Punkte
🔧 Programmierung

🔧 Docker Commands Cheat Sheet


📈 34 Punkte
🔧 Programmierung

🔧 Windows CMD Commands Cheat Sheet


📈 34 Punkte
🔧 Programmierung