Here's a cheat sheet for essential Git commands:
Configuration:
- Configure Git user information:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Creating Repositories:
-
Initialize a new Git repository:
git init
-
Clone an existing repository:
git clone <repository_url>
Basic Workflow:
-
Check the status of your working directory:
git status
-
Add changes to the staging area:
git add <file>
(orgit add .
to add all changes)
-
Commit changes with a message:
git commit -m "Your commit message"
-
Push changes to a remote repository:
git push
Branches:
-
Create a new branch:
git branch <branch_name>
-
Switch to a different branch:
git checkout <branch_name>
-
Create a new branch and switch to it:
git checkout -b <new_branch_name>
-
List all branches (local and remote):
git branch -a
-
Merge a branch into the current branch:
git merge <branch_name>
Updating and Synchronizing:
-
Pull changes from a remote repository:
git pull
-
Fetch changes from a remote repository:
git fetch
Reviewing History:
-
View commit history:
git log
-
View a simplified, one-line commit history:
git log --oneline
-
View a graphical commit history:
git log --graph --oneline --all
Undoing Changes:
-
Discard changes in the working directory:
git checkout -- <file>
-
Unstage changes:
git reset <file>
-
Amend the last commit (for small changes):
git commit --amend
Remote Repositories:
-
Add a remote repository:
git remote add <remote_name> <repository_url>
-
Remove a remote repository:
git remote remove <remote_name>
Tagging:
-
Create an annotated tag:
git tag -a <tag_name> -m "Tag message"
-
Push tags to the remote repository:
git push --tags
Collaboration:
-
Fetch changes from a remote repository:
git fetch
-
Create a pull request (GitHub/GitLab):
- Use the web interface of your platform
-
Merge a pull request (GitHub/GitLab):
- Use the web interface of your platform
Git Help:
- Get help for a specific command:
git help <command>
orgit <command> --help
Remember that Git is a powerful tool with many more commands and options, but these are the essential commands to get you started with version control and collaboration.