Git is an essential tool for developers to manage their source code. Mastering Git commands will significantly improve your productivity when working on projects, especially in a collaborative environment. Below are the 10 must-know Git commands that every developer should be familiar with.
Usage: This command is used to initialize a new Git repository. It creates a hidden .git
directory in the current folder, allowing you to start tracking changes.
git init
Example: After running git init
in a folder, Git will begin tracking changes in that folder.
Usage: This command is used to copy a Git repository from a remote server (e.g., GitHub, GitLab) to your local machine. It also initializes the repository locally.
git clone https://github.com/username/repository.git
Example: Cloning a repository from GitHub will download all the files and the commit history to your local machine.
Usage: The git status
command displays the current state of your working directory and staging area. It shows which files have been modified, added, or deleted.
git status
Example: Running git status
will show you if any files are staged for commit or untracked changes in the repository.
Usage: Use git add
to add changes in the working directory to the staging area, preparing them to be committed. You can add individual files or entire directories.
git add filename.txt
Example: To add all files at once, you can use git add .
to stage all modified and new files in the working directory.
Usage: The git commit
command records changes in the repository's history. Commits include a descriptive message to explain the changes.
git commit -m \"Your commit message\"
Example: Committing after staging changes will save the snapshot of the repository's current state.
Usage: This command sends your committed changes to a remote repository (e.g., GitHub). It's used to sync your local repository with the remote repository.
git push origin main
Example: Pushing changes to the main
branch will upload your local changes to the remote repository.
Usage: The git pull
command fetches and integrates changes from the remote repository into your local repository. It is essentially a combination of git fetch
and git merge
.
git pull origin main
Example: Pulling changes from the main
branch will update your local repository with any changes made in the remote repository.
Usage: This command lists all the branches in your repository. You can also use it to create or delete branches.
git branch
Example: To create a new branch, you can use git branch feature-branch
. To switch to that branch, use git checkout feature-branch
.
Usage: The git checkout
command is used to switch between branches or restore files in your working directory.
git checkout feature-branch
Example: This command allows you to work on different features or stages of your project by switching between branches.
Usage: This command is used to combine the changes from one branch into another. Typically, you would use this to merge a feature branch into the main
branch after completing a feature.
git merge feature-branch
Example: Merging the feature-branch
into the main
branch combines the changes and updates the history.
These 10 Git commands are the foundation for efficiently managing version control in any project. Whether you're collaborating with a team or working alone, knowing these commands will make your development workflow smoother and more productive.