As a software developer, version control is an essential aspect of managing code. Git is a widely used version control system that allows developers to track changes made to code and collaborate with others.
In this post, I’ll share Git commands I use every day to make my development experience smoother and more efficient. Whether you’re a new developer or an experienced one, these commands can help you become more effective, confident, and disciplined in handling your Git workflow. Let’s dive in!
Git Clone
The git clone command is used to create a copy of a Git repository on your local machine. This command is used when you want to start working on a project that already exists in a remote repository. To clone a repository, you need to provide the URL of the remote repository.
git clone <https://github.com/username/repository.git>
Git Pull
The git pull command is used to update your local repository with the latest changes from the remote repository. This command is used when you are working on a project with other developers and want to ensure that you have the latest code. It is recommended to run git pull frequently to avoid conflicts.
git pull
Git Add
The git add command is used to stage changes made to your local repository. When you make changes to your code, Git does not automatically track those changes. You need to add the changes to the staging area before committing them.
git add file1.js file2.js
Git Commit
The git commit command is used to save changes made to the local repository. When you commit changes, you need to provide a commit message that describes the changes made. It is recommended to write clear and descriptive commit messages.
git commit -m "Added new feature to the app"
Git Push
The git push command is used to upload local changes to the remote repository. When you push changes, other developers working on the project can see your changes and collaborate with you. It is recommended to push changes frequently to ensure that the remote repository is up to date.
git push
Git Branch
The git branch command is used to create, list, and delete branches in the repository. Branches are used to isolate changes made to the code and to work on multiple features simultaneously. It is recommended to create a new branch for each new feature.
git branch new-feature
Git Merge
The git merge command is used to combine changes made on different branches. When you merge a branch, the changes made on that branch are applied to the current branch. It is recommended to merge branches frequently to avoid conflicts.
git merge new-feature
Git Status
The git status command is used to display the current status of the repository. It shows which files have been modified and which files have been staged for commit. It is recommended to run git status frequently to keep track of changes made to the repository.
git status
Git Checkout
The git checkout command is used to switch between branches in the repository. This command is useful when you want to work on a different branch or view the code on a different branch.
git checkout new-feature
This command can also be used to create a new branch and switch to it in one step.
git checkout -b new-branch-name
So, when you run git checkout -b new-branch-name, you are telling Git to create a new branch named "new-branch-name" and switch to it. This is useful when you want to start working on a new feature or fixing a bug without affecting the main branch or any other branch in the repository.
There’s faster way to switch/toggle between two branch using the checkout command.
git checkout -
Suppose you are currently in branch-1 and switched to branch-2. If you want to get back to the branch-1 and again if you want to switch back to the branch-2 then the below command will be very useful.
Git Stash
The git stash command is used to temporarily store changes made to the repository. This command is useful when you want to switch branches or work on a different feature without committing the changes.
Usage
To stash changes, use the following command:
git stash
This will temporarily store the changes made to the repository.
Show Stash list
To show the list of stashes, use the following command:
git stash list
This will show the list of stashes.
Drop a stash
To drop a stash, use the following command:
git stash drop <stash_id>
This will drop the stash with the specified stash_id.
Apply a stash
To apply a stash, use the following command:
git stash apply <stash_id>
This will apply the changes from the stash with the specified stash_id.
Pop a stash
To apply and drop a stash at the same time, use the following command:
git stash pop <stash_id>
This will apply the changes from the stash with the specified stash_id and drop the stash.
Conclusion
Git is a powerful version control system that allows developers to manage code efficiently. The commands I discussed in this blog post are very basic and essential and I use these commands daily as a software developer. By using these commands regularly, you can ensure that your code is well-managed and that collaboration with other developers is smooth and efficient.
