DEV Community

Cover image for Must know git commands for a developer
Kumar Kalyan
Kumar Kalyan

Posted on • Updated on • Originally published at dev.to

Must know git commands for a developer

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.

init

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, Un versioned project to a Git repository, and to do these things you can use

git init

clone

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

add

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

branch

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

status

Suppose you are working on a repository and you want to see what are the changes that occurred, then you will use

git status

to get all the necessary info about
The current branch is up to date or not,
Is there anything to commit, push or pull,
Any files are being created, modified, deleted, staged, unstaged or untracked

log

Suppose you want to know about the history of what is done to your repository, then use

git log

and it will show you the history of everything that happens to a repository

checkout

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.

pull

It is used to get updates from the remote repo. we use

git pull

to get the updates from a remote repository (git fetch) and immediately applies the latest changes in your local (git merge).

revert

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

merge

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, the use the

git merge

to merge the new features

commit

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”

push

After committing, if your code is ready for production then use the

git push <remote> <branch-name>

and this will push all the changes

Conclusion

So these are the basic commands of git you must know as a developer ..
Just comment out if I have left something …
Happy coding:)

Connect



Top comments (3)

Collapse
 
sillva profile image
Gui Silva

I'd also include rebase in this list.

Collapse
 
kumarkalyan profile image
Kumar Kalyan

thanks for your comment :)

Collapse
 
willworth profile image
Will Worth

nevermind = !git reset --hard HEAD && git clean -d -f

removes all new files added and /or changed since the clean situation, and checks out fresh. If only it worked outside git...