💡 Introduction
Git is an essential tool for devs, enabling efficient version control and collaboration. This guide provides an overview of the most important Git commands to help you manage repositories, track changes, and collaborate with your team.
⚙️ Prerequisites
Before getting started, ensure that:
- Git is installed on your system.
- You have basic knowledge of the command line.
✍️ Getting Started
Before using Git, set up your username and email so your commits are properly attributed.
git config --global user.name "Danilo Mello"
git config --global user.email "danilo@email.com"
To check your configurations:
git config user.name
git config user.email
git config --list
You can also create aliases to simplify commands:
git config --global alias.s status
👉 Creating and Managing Repositories
Initializing a Repository
git init
Adding a Remote Repository
git remote add origin git@github.com:username/reponame.git
Cloning a Repository
git clone git@github.com/username/reponame.git foldername
Checking Remote Repositories
git remote -v
👉 Tracking File Changes
Checking File Status
git status
Staging Files
git add filename
git add . # Add all files
Committing Changes
git commit -m "My first commit"
git commit -am "Add and commit changes"
To commit specific changes interactively:
git add -p
👉 Analyzing Commit History
Viewing Commit Logs
git log
git log --author="Danilo"
git log --since="Jan 01 2025" --until="March 01 2025"
git log --graph
Viewing Short Logs
git shortlog -sn
Viewing Specific Commit Details
git show commitHashHere
Comparing Changes
git diff
git diff --name-only
👉 Managing Commits
Amending the Last Commit
git commit --amend
Undoing Changes
git checkout filename
git reset HEAD filename
Resetting Commits
git reset --soft commitHash # Moves changes to staging
git reset --mixed commitHash # Moves changes to modified
git reset --hard commitHash # Removes changes permanently
Applying a Specific Commit to Another Branch
git cherry-pick commitHash
Combining Commits
git rebase -i HEAD~2
👉 Working with Branches
Creating and Switching Branches
git checkout -b branchName
git checkout branchName
git checkout -
Listing and Deleting Branches
git branch
git branch -D branchName
Merging and Rebasing Branches
git merge branchName
git rebase branchName
Pulling Changes with Rebase
git pull origin main --rebase
Resolving Conflicts
git merge --continue
git rebase --continue
👉 Saving Work Without Committing
Stashing Changes
git stash
Applying Stashed Changes
git stash apply
git stash list
git stash clear
👉 Pushing and Managing Remote Changes
Pushing Changes to a Repository
git push origin main
Tagging Releases
git tag -a 1.0.0 -m "First release"
git push origin main --tags
Reverting a Push
git revert commitHash
🤝 Conclusion
Mastering Git commands is essential for efficient development and collaboration. By using these commands regularly, you'll improve your workflow and avoid common version control issues.
If this guide helped you, let me know your feedback! Share it with a friend or post it on social media to help others improve their Git skills. Thank you for reading! 🚀