DEV Community

Liz P
Liz P

Posted on

Git Commands (Do I know anything besides add, commit and push?)

I was in a group zoom chat the other day and one of the other participants mentioned they only knew about 4 git commands: git add, git commit, git push and git pull. It got me thinking, do I know any more than that? Um, maybe two or three more, creating a new branch and switching branches, oh and git status to check what’s going on. Then I got to questioning, do I need to know more than this? The answer is probably yes, it would be good to know more.


First, what’s Git? Why do we use git and git commands? Git is a distributed version control system that helps developers collaborate on projects of any scale. Git makes it possible to track changes to our code and go back to a previous version if we make a mistake…because who hasn’t, amirite? Git also makes collaboration easy.

Git status- let’s us know what’s happening with our current branch. Is our branch up to date, do we have files that are created, changed, or deleted, do we have files that are unstaged, staged or untracked.

git status 
Enter fullscreen mode Exit fullscreen mode

Git add- stage changes to file(s) for our next commit.

Add single file:

git add <file name>
Enter fullscreen mode Exit fullscreen mode

Add all files:

git add .
Enter fullscreen mode Exit fullscreen mode

Git commit- everyone knows this one, this will save/commit changes that we’ve just staged. We should add a brief message to describe what we did.

git commit -m ‘write your message here’ 
Enter fullscreen mode Exit fullscreen mode

Git push- once we’ve committed our changes, we want to push these changes to our remote repository.

git push 
Enter fullscreen mode Exit fullscreen mode

Git pull- gets updates from the remote repository, does the combined actions of git fetch and git merge. Note this may cause conflicts that will need to be resolved manually.

git pull
Enter fullscreen mode Exit fullscreen mode

If you have a conflict that can’t be resolved or you want to quit the merge you can run:

git merge  --abort
Enter fullscreen mode Exit fullscreen mode

This will take you back to the branch before you pulled.

Git branch- by default you've got a single branch, the main branch. By creating and using different branches you're able to have multiple people working simultaneously.

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch:

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

Push branch to remote repo:

git push -u <remote> <branch-name>
Enter fullscreen mode Exit fullscreen mode

Delete a branch:

git -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Switch to a branch:

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

These are only a few of the many git commands available to us as developers. I came across many git cheat sheets and there are so many resources listing the different commands and explaining their purpose. I highly recommend checking out the following:

https://docs.github.com/en/get-started/using-git
https://www.gitkraken.com/learn/git/commands
https://git-scm.com/docs/gittutorial
https://education.github.com/git-cheat-sheet-education.pdf

These helped me and this article! I think the more you use these commands, the more comfortable you get using them. I look forward to continuing to expand my knowledge of git commands. Feel free to share any git commands I didn’t cover that you think are important. Thanks for reading!

Top comments (0)