Basic Commands
- Initialize a repository
git init
- Clone a repository
git clone <repository_url>
- Check repository status
git status
- Add files to staging area
git add <file>
git add .
- Commit changes
git commit -m "commit message"
- Add and commit changes
git commit -am "commit message"
Branching
- List branches
git branch
- 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>
- Merge a branch
git checkout <target_branch>
git merge <branch_to_merge>
Remote Repositories
- Add a remote repository
git remote add <remote_name> <repository_url>
- List remote repositories
git remote -v
- Fetch changes from remote
git fetch <remote_name>
- Pull changes from remote
git pull <remote_name> <branch_name>
- Push changes to remote
git push <remote_name> <branch_name>
Undoing Changes
- Undo changes in working directory
git checkout -- <file>
- Unstage changes
git reset HEAD <file>
- Undo last commit but keep changes
git reset --soft HEAD~1
- Undo last commit and discard changes
git reset --hard HEAD~1
- Revert a commit
git revert <commit_id>
Viewing History
- Show commit history
git log
- Show commit history with changes
git log -p
- Show a specific commit
git show <commit_id>
- Show changes to a file
git log -p <file>
Stashing Changes
- Stash changes
git stash
- List stashes
git stash list
- Apply a stash
git stash apply
- Apply and drop a stash
git stash pop
- Drop a stash
git stash drop
Tagging
- Create a tag
git tag <tag_name>
- List tags
git tag
- Push tags to remote
git push <remote_name> <tag_name>
Helpful Tips
- Alias for common commands
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
- Configure user information
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Keep this cheatsheet handy for quick reference while working with Git!