Git plays an important role in the daily workflow of programmers, particularly when collaborating within a team, and it stands as an indispensable tool in the software industry. Having a solid grasp of Git commands is essential for efficient version control and collaboration.
In this post, I will share the top 14 Git commands that every developer should know.
Let’s get started!
1.git init
The git init command is used to initialize a new repository in your project folder.
git init
2.git clone
The git clone command is used to clone an existing git repository onto your local machine. By cloning a repository you can start collaborating on an existing project.
git clone <repository_url>
Replace <repository_url>
with the URL of the repository which you want to clone.
3.git branch
The git branch command is used to create, list and delete branches.
Creating a new branch:
You can use the following command to create a branch locally.
git branch <new_branch_name>
Replace <new_branch_name>
with your new branch name.
Listing branches:
git branch
Deleting a branch:
git branch -d <branch_name>
Replace <branch_name>
with your branch name.
4.git checkout
The git checkout command is used to switch between branches.
git checkout <branch_name>
To switch between branches, ensure that any changes in your current branch are either committed or stashed before making the switch. Additionally, make sure the branch you intend to check out already exists locally.
You can use the following command to seamlessly branch creation and switch in one go.
git checkout -b <branch_name>
5.git add
The git add command is used to stage changes of a specific file or all changes for the next commit.
For a specific file:
git add <file_name>
Replace <file_name>
with your file name.
For all changes:
git add .
6.git reset
The git reset command is used to unstage changes of a file.
git reset <file_name>
Replace <file_name>
with your file name.
7.git commit
The git commit command is used to commit staged changes with a descriptive message.
git commit -m "Your message"
8.git status
The git status command is used to check the status (such as untracked files and changes ready to be committed) of your repository.
git status
9.git log
The git log command is used to display a history of all the commits. It includes commit hashes, authors, dates, and commit messages.
git log
10.git diff
The git diff command is used to check the differences between your current working directory and the last commit.
git diff
11.git push
The git push command is used to send your commits to the remote repository. This will only push the committed changes.
git push origin <branch_name>
If your branch is newly created then you will need to upload the branch with the following command.
git push -u origin <branch_name>
12.git pull
The git pull command is used to fetch changes from a remote repository and integrate them into your local branch.
It is essentially a combination of two separate Git commands: git fetch and git merge.
git pull origin
13.git stash
The git stash command is used to temporarily save changes that you’re not ready to commit.
git stash
14.git merge
The git merge command combines all branch changes into one branch.
git merge <branch_name>
Replace <branch_name>
with the branch name whose changes you want to merge into the current branch.
To merge the branches you first need to checkout to the branch in which you want to merge all the branches.
Conclusion
These essential Git commands should be your constant companions on your coding journey. Whether you’re starting a new project, collaborating with a team, or fixing bugs, these commands will guide you through version control, simplifying your development journey and keeping things running smoothly.
Thanks for reading.
For more content like this click here.
Top comments (0)