Essential Git Commands
Here’s a list of commonly used Git commands, categorized for convenience:
1. Setup and Configuration
- Check Git Version:
git --version
- Configure Username:
git config --global user.name "Your Name"
- Configure Email:
git config --global user.email "your_email@example.com"
- View Configuration:
git config --list
2. Repository Management
- Initialize a Repository:
git init
- Clone a Repository:
git clone <repository-url>
- View Repository Status:
git status
3. Working with Files
- Add Files to Staging Area:
git add <file> # Add a specific file
git add . # Add all changes in the current directory
- Commit Changes:
git commit -m "Your commit message"
- Remove Files:
git rm <file>
- Move/Rename Files:
git mv <old_filename> <new_filename>
4. 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:
git merge <branch-name>
- Delete a Branch:
git branch -d <branch-name>
5. Remote Repositories
- Add a Remote Repository:
git remote add origin <repository-url>
- View Remote Repositories:
git remote -v
- Push Changes to Remote Repository:
git push origin <branch-name>
- Pull Changes from Remote Repository:
git pull origin <branch-name>
- Fetch Changes from Remote:
git fetch
6. Viewing and Inspecting
- View Commit History:
git log
- View a Specific Commit:
git show <commit-hash>
- View Changes in Files:
git diff
- View Changes in Staged Files:
git diff --staged
- List Branches:
git branch
7. Undo Changes
- Unstage Files:
git reset <file>
- Undo Last Commit (Keep Changes):
git reset --soft HEAD~1
- Undo Last Commit (Discard Changes):
git reset --hard HEAD~1
- Discard Unstaged Changes:
git checkout -- <file>
8. Stashing
- Save Changes Temporarily:
git stash
- Apply Stashed Changes:
git stash apply
- List Stashes:
git stash list
- Drop a Stash:
git stash drop
9. Tags
- Create a Tag:
git tag <tag-name>
- List Tags:
git tag
- Push Tags to Remote:
git push origin <tag-name>
10. Collaboration
- Rebase:
git rebase <branch-name>
- Cherry-pick a Commit:
git cherry-pick <commit-hash>
-
Resolve Merge Conflicts:
- Edit conflicting files manually.
- Stage resolved files:
git add <file>
- Complete the merge:
git commit
These commands cover most Git functionalities for beginners and intermediate users. If you need clarification or advanced commands, feel free to ask! 😊
Top comments (2)
you missed
git pull --rebase
Fetches the latest changes from the remote branch and then applies your local commits on top of them - pretty useful right
Great point! Let me clarify what git pull --rebase does:
When you use git pull --rebase, Git first fetches the latest changes from the remote branch (just like git pull normally does). But instead of merging them with your local commits, it rebases your commits on top of the remote ones. This effectively rewrites your commit history to make it look like your changes were applied after the remote changes.
This is especially helpful when you want to avoid creating unnecessary merge commits, which can clutter the history. By rebasing, you keep the commit history cleaner and linear, making it easier to read and follow.
Example Use Case:
Before Rebase:
You and a colleague are working on the same branch. Your colleague has pushed some changes, and you’ve made your own local commits. Now, you want to pull the changes and apply your local commits on top, rather than merging them, to avoid the extra merge commit.
With git pull --rebase:
git pull --rebase will fetch the changes from the remote branch, then reapply your commits on top of them, as if your work was based on the most up-to-date version of the branch.
Benefits of Using git pull --rebase:
Cleaner Commit History: Avoids unnecessary merge commits.
Easy to Read: Makes it easier for others to follow the project’s history.
Better Collaboration: Keeps your work in sync with others without the clutter of merge commits.
So, yes, git pull --rebase is indeed a handy command for maintaining a tidy and efficient Git workflow!