Git is an essential tool for modern software development, enabling version control, collaboration, and project management. Familiarity with Git commands is crucial for developers at all levels. Here are some of the top Git commands every developer should know:
- git init
This command initializes a new Git repository in the current directory.
“`bash
git init
“`
- git clone
This command creates a copy of an existing Git repository. It’s used to download a repository from a remote source.
“`bash
git clone <repository-url>
“`
- git status
Displays the current state of the working directory and the staging area. It shows which files have been modified, added, or are untracked.
“`bash
git status
“`
- git add
Adds changes from the working directory to the staging area. You can add specific files or all changes.
– To add a specific file:
“`bash
git add <filename>
“`
– To add all changes:
“`bash
git add .
“`
- git commit
Records changes in the repository. You can use the `-m` flag to include a message describing the commit.
“`bash
git commit -m “Your commit message here”
“`
- git log
Shows the commit history for the current branch, providing a timeline of changes, including commit hashes, authors, dates, and messages.
“`bash
git log
“`
- git diff
Displays the differences between files in the working directory and the staging area or between different commits.
– To see changes in the working directory:
“`bash
git diff
“`
- git branch
Lists all branches in the repository. You can also create or delete branches with this command.
– To list branches:
“`bash
git branch
“`
– To create a new branch:
“`bash
git branch <branch-name>
“`
- git checkout
Switches to a different branch or restores files in the working directory to match a specific commit.
– To switch branches:
“`bash
git checkout <branch-name>
“`
– To create and switch to a new branch in one command:
“`bash
git checkout -b <branch-name>
“`
- git merge
Combines changes from one branch into another. This command is typically used to merge a feature branch back into the main branch.
“`bash
git merge <branch-name>
“`
- git pull
Fetches changes from a remote repository and merges them into the current branch. This command is a combination of `git fetch` and `git merge`.
“`bash
git pull origin <branch-name>
“`
- git push
Uploads local commits to a remote repository. It is used to share changes with others on the same project.
“`bash
git push origin <branch-name>
“`
- git remote
Manages remote repositories associated with your local repository. You can list, add, or remove remote repositories.
– To list remote repositories:
“`bash
git remote -v
“`
- git reset
Resets the current branch to a specified commit. You can control whether you want to keep changes in the working directory or not.
– To discard changes:
“`bash
git reset –hard <commit-hash>
“`
– To keep changes in the working directory:
“`bash
git reset <commit-hash>
“`
- git stash
Temporarily saves changes in your working directory that are not ready to be committed, allowing you to revert to a clean working state. You can later apply or inspect these changes.
“`bash
git stash
“`
– To reapply the stashed changes:
“`bash
git stash pop
“`
Conclusion
Familiarizing yourself with these essential Git commands will significantly enhance your efficiency and effectiveness as a developer. Beyond these commands, consider exploring advanced features like rebasing, squashing commits, and configuring Git hooks to further streamline your workflow.