25 Useful Git Commands Everyone Should Know
Git is a powerful tool for version control that allows developers to track changes in their codebase, collaborate with others, and manage projects efficiently. Whether you’re a beginner or an experienced developer, knowing the right Git commands can save you time and effort. Below are 25 essential Git commands that will help you navigate and manage your repositories effectively.
1. git init
The git init
command initializes a new Git repository in your current directory. It creates a .git
subdirectory, which contains all the necessary metadata for your repository.
Usage:
git init
This command is often the first step when starting a new project.
2. git clone
The git clone
command copies an existing repository from a remote server to your local machine. It also sets up a connection to the remote repository.
Usage:
git clone <repository-url>
This is commonly used to start working on an existing project.
3. git add
The git add
command stages changes (new files or modifications) for the next commit. It prepares your changes to be tracked by Git.
Usage:
git add <file-name>
To stage all changes, use:
git add .
4. git commit
The git commit
command saves your staged changes to the repository history. Each commit represents a snapshot of your project at a specific point in time.
Usage:
git commit -m "Your commit message"
It’s essential to write clear and descriptive commit messages.
5. git status
The git status
command shows the current state of your working directory and staging area. It lists which files are modified, staged, or untracked.
Usage:
git status
This command helps you keep track of your changes before committing.
6. git log
The git log
command displays the commit history for the repository. It shows each commit’s hash, author, date, and message.
Usage:
git log
This is useful for reviewing the history of changes in your project.
7. git branch
The git branch
command lists all branches in your repository. It also indicates the current branch you are working on.
Usage:
git branch
To create a new branch, use:
git branch <branch-name>
8. git checkout
The git checkout
command switches between branches or restores files to a previous state. It’s often used for navigating different parts of your project.
Usage:
git checkout <branch-name>
To switch to a specific commit, use:
git checkout <commit-hash>
9. git merge
The git merge
command combines changes from one branch into another. It’s commonly used to integrate new features or updates into the main branch.
Usage:
git merge <branch-name>
This command is essential for collaborative work.
10. git pull
The git pull
command fetches changes from a remote repository and merges them into your current branch. It’s a combination of git fetch
and git merge
.
Usage:
git pull origin <branch-name>
This ensures your branch is up to date with the remote repository.
11. git push
The git push
command uploads your local commits to a remote repository. It’s used to share your work with others.
Usage:
git push origin <branch-name>
Always pull before pushing to avoid conflicts.
12. git fetch
The git fetch
command downloads commits, files, and references from a remote repository without merging them. It updates your local copy with the latest changes.
Usage:
git fetch
This command is useful for checking changes before merging them.
13. git remote
The git remote
command manages connections to remote repositories. It allows you to add, remove, and list remotes.
Usage:
git remote -v
To add a new remote, use:
git remote add <name> <url>
14. git diff
The git diff
command shows the differences between commits, branches, or your working directory and staging area.
Usage:
git diff
This command helps you review changes before committing.
15. git stash
The git stash
command temporarily saves changes that are not ready to be committed. It allows you to work on something else without losing your progress.
Usage:
git stash
To apply the stashed changes, use:
git stash apply
16. git rebase
The git rebase
command moves or combines commits from one branch to another. It’s often used to maintain a linear project history.
Usage:
git rebase <branch-name>
This command is powerful but should be used carefully.
17. git tag
The git tag
command creates a tag, which is a reference to a specific commit. Tags are often used to mark release points.
Usage:
git tag <tag-name>
To push tags to a remote repository, use:
git push origin <tag-name>
18. git show
The git show
command displays information about a specific commit, tag, or object. It’s useful for inspecting changes in detail.
Usage:
git show <commit-hash>
This command provides detailed insights into individual changes.
19. git reset
The git reset
command undoes changes by moving the HEAD to a previous commit. It’s often used to unstage files or discard commits.
Usage:
git reset <commit-hash>
Be cautious when using this command, as it can alter your commit history.
20. git rm
The git rm
command removes files from the working directory and stages the removal for the next commit.
Usage:
git rm <file-name>
This command is used when you want to delete files from your project.
21. git blame
The git blame
command shows which commit and author last modified each line of a file. It’s helpful for tracking changes and understanding code history.
Usage:
git blame <file-name>
This command is useful for code reviews and debugging.
22. git cherry-pick
The git cherry-pick
command applies a specific commit from one branch to another. It’s often used to apply hotfixes.
Usage:
git cherry-pick <commit-hash>
This command is great for selectively applying changes.
23. git archive
The git archive
command creates a zip or tarball archive of your repository. It’s useful for distributing source code.
Usage:
git archive --format=zip HEAD > archive.zip
This command allows you to package your project easily.
24. git bisect
The git bisect
command helps you find the commit that introduced a bug. It uses a binary search to efficiently locate the problematic commit.
Usage:
git bisect start
This command is particularly useful for debugging large projects.
25. git clean
The git clean
command removes untracked files from your working directory. It’s useful for cleaning up your project and keeping it tidy.
Usage:
git clean -f
To remove directories as well, use:
git clean -fd
Conclusion
Understanding and mastering these Git commands can greatly improve your efficiency in managing code repositories. Whether you’re collaborating on a team project or working solo, these commands will help you stay organized and productive. By integrating these commands into your workflow, you’ll be well-equipped to handle any version control challenge that comes your way.
Thank you for reading the article! If you found the information useful, you can donate using the buttons below:
Donate ☕️ with PayPalDonate 💳 with Revolut