Whether you are a professional developer or a beginner in Computer Science, you often have heard the words git and GitHub, as nowadays git has become a daily need in our life as a developer, and is widely used in the software industry.
Since Git is not easy to master as it has many commands but, in this article, I am going to share a few often-used git commands which you must be aware of as a developer.
Suppose you have written some code and now you want to initialize it as a new Git repository, or you want to convert an existing, unversioned project to a Git repository, and to do these things you can use
Suppose you want to download all the source code of a particular project which you have found on Github, using the command line then you can use
git clone <https://url-of-the-repository>
and this will download all the source code of the given repository URL
Suppose you have created a new file or whether you have modified or deleted an existing one and you want to commit these changes to your GitHub repo then you can use
for committing a single file
git add <file-name>
or you can use
git add -A
to commit all the changes
Branches are mostly used in Git as it helps developers work in parallel to a particular project. We can use the git branch command for creating, listing, and deleting branches.
Suppose you have a product having features a, b & c, now you want to add a new featured ‘d’ to it. Instead of directly adding the new feature to our main code we can create a branch and add the new feature to it now after all the tests you can simply merge it to the master branch. Here is the list of git branch commands which you can use
git branch <branch-name >
will create a new branch
git branch — list
will show you the list of all the branches in a repo
git branch -d <branch-name>
will delete a branch with the given name
Suppose you are working on a repository and you want to see what are the changes that occurred, then you will use
to get all the necessary info about
Suppose you want to know about the history of what is done to your repository, then use
and it will show you the history of everything that happens to a repository
suppose there are three branches A, B, C and you want to work on branch ‘A’, and to do so you can use
git checkout <branch-name >
and this will help you to switch on a particular branch
We can also use it for checking out files and commits.
It is used to get updates from the remote repo. we use
to get the updates from a remote repository (git fetch) and immediately applies the latest changes in your local (git merge).
Sometimes you may want to undo all your last changes to your project, in that case, we can use the
git revert <commit if>
to undo all the changes in a particular commit id
If you’re done with development inside your feature branch and tested your code, you can merge your branch with the parent branch. This could be either a develop branch or a master branch depending on the git workflow you follow, then use the
to merge the new features
Suppose you have done all the modifications and changes to your project and now you want to save all these, then you can use the
git commit -m “Add a commit message”
After committing, if your code is ready for production then use the
git push <remote> <branch-name>
and this will push all the changes
So these are the basic commands of git you must know as a developer.
Just comment out if I have left something