Comprehensive Guide to Git Commands
1. Setting Up a Repository
Initialize a Git repository:
git init
Clone an existing repository:
git clone https://github.com/user/repository.git
2. Staging and Committing Changes
Check the status of your repository:
git status
Stage files for commit:
git add <file-name>
git add .
git add *.js
Commit changes:
git commit -m "Commit message describing the change"
3. 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>
Merge a branch into the current branch:
git merge <branch-name>
Delete a branch after merging:
git branch -d <branch-name>
4. Pushing and Pulling
Push changes to a remote repository:
git push origin <branch-name>
Pull the latest changes from a remote repository:
git pull origin <branch-name>
5. Handling Merge Conflicts
After a conflict, check the status:
git status
Manually resolve conflicts:
git add <resolved-file>
Continue with the merge after resolving conflicts:
git commit
6. Rebasing
Rebase a branch:
git rebase <branch-name>
7. Resetting Changes
Unstage changes:
git reset <file-name>
Discard local changes in a file:
git checkout -- <file-name>
Reset to a previous commit:
git reset --hard <commit-hash>
8. Stashing Changes
Stash your changes:
git stash
Apply stashed changes:
git stash apply
List all stashed changes:
git stash list
9. Tagging
Create a new tag:
git tag <tag-name>
Push a tag to a remote repository:
git push origin <tag-name>
10. Viewing Commit History
View the commit log:
git log
View a graphical representation of the log:
git log --graph --oneline --decorate --all
11. Reverting Changes
Revert a commit:
git revert <commit-hash>
12. Squashing Commits
Interactive rebase to squash commits:
git rebase -i HEAD~n
13. Cleaning Up Untracked Files
Remove untracked files:
git clean -f
Remove untracked directories:
git clean -fd
14. Git Aliases
Create a Git alias:
git config --global alias.st status
15. Checking Out Previous Commits
Checkout a previous commit:
git checkout <commit-hash>
16. Forking and Pull Requests
Fork a repository:
Click the "Fork" button on GitHub.
Clone your forked repository:
git clone https://github.com/your-username/repository.git
Create a new branch for your changes:
git checkout -b feature-branch
Make changes and push to your fork:
git push origin feature-branch
Create a Pull Request:
On GitHub, navigate to your repository and click "New Pull Request."
17. Working with Remotes
Add a remote repository:
git remote add origin https://github.com/user/repository.git
View remote repositories:
git remote -v
Remove a remote repository:
git remote remove origin
18. Git Bisect
Start bisecting:
git bisect start
Mark the current commit as bad:
git bisect bad
Mark an earlier commit as good:
git bisect good <commit-hash>
19. Cherry-Picking Commits
Cherry-pick a commit:
git cherry-pick <commit-hash>
20. Reflog
View the reflog:
git reflog
Recover a commit using reflog:
git checkout <commit-hash>