Git is a distributed version control system that allows developers to track changes, collaborate on projects, and manage source code efficiently. Whether you're a beginner or an experienced developer, having a cheat sheet handy can be immensely helpful. In this article, we'll explore a comprehensive Git cheat sheet that covers all the essential commands and workflows.
Configuration
-Set up your name
git config --global user.name "Your Name"
-Set up your email
git config --global user.email "youremail@example.com"
-Configure default text editor
git config --global core.editor "editor_name"
Repository Creation
-Initialize a new repository
git init
-Clone an existing repository
git clone <repository_url>
-Initialize a new repository with a README
git init <directory_name> --initial-branch=<branch_name>
-Clone a repository to a specific directory
git clone <repository_url> <directory_name>
Cloning Repositories
-Clone a repository
git clone <repository_url>
-Clone a repository to a specific directory
git clone <repository_url> <directory_name>
Basic Workflow
-Check repository status
git status
-Stage changes for commit
git add <file_name> # Or use "." to stage all changes
-Unstage changes
git restore --staged <file_name>
-Commit changes
git commit -m "Commit message"
-Amend the last commit
git commit --amend
-Push changes to remote repository
git push origin <branch_name>
-Pull changes from remote repository
git pull origin <branch_name>
-Fetch changes from remote repository
git fetch origin
Branching and Merging
-Create a new branch
git branch <branch_name>
-Switch to a branch
git checkout <branch_name>
-Create and switch to a new branch
git checkout -b <branch_name>
-List all branches
git branch
-Delete a branch
git branch -d <branch_name>
-Merge branches
git merge <branch_name>
-Resolve merge conflicts: Open conflicted files, manually resolve conflicts, and commit changes.
Remote Repositories
-Add a remote repository
git remote add <remote_name> <remote_url>
-List remote repositories
git remote -v
-Remove a remote repository
git remote remove <remote_name>
Collaborative Workflows
-Push a branch to a remote repository
git push origin <branch_name>
-Create a pull request (PR) on GitHub/Bitbucket
-Review and merge PR on GitHub/Bitbucket
Stashing Changes
-Stash changes
git stash
-List stashed changes
git stash list
-Apply stashed changes
git stash apply <stash_id>
-Clear all stashed changes
git stash clear
Inspecting and Comparing
-View commit history
git log
-View file changes
git diff
-View changes between commits
git diff <commit_hash1> <commit_hash2>
-View branches' commit history
git log --graph --oneline --decorate --all
Undoing Changes
-Undo the last commit, keeping changes
git reset HEAD~1
-Discard changes in a file
git checkout -- <file_name>
-Revert a commit
git revert <commit_hash>
Gitignore
-Create a .gitignore file
touch .gitignore
-Specify files and directories to ignore in the .gitignore file
-Use patterns and wildcards to match files/directories
This comprehensive Git cheat sheet provides an overview of essential commands and workflows for developers. By familiarizing yourself with these commands, you'll be able to effectively manage your source code, collaborate with team members, and track changes in your projects.