Here Are 7 Git Commands Every Developer Should Know

Git is a one-size-fits-all tool that lets developers share and organize their code

Image for post
Image for post
Photo by Yancy Min on Unsplash

git add

The git add command lets you commit files to your repository. Quintessiall to everyday development, especially when working in a team. Every employer expects you to know how to add files to a repository.

Image for post
Image for post
git add

Syntax

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
[--dry-run] [(-c | -C | --fixup | --squash) <commit>]
[-F <file> | -m <msg>] [--reset-author] [--allow-empty]
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
[-i | -o] [-S[<keyid>]] [--] [<file>…​]

git commit

git commit lets you create a new commit containing the current contents of the index and the given log message describing the changes.

Image for post
Image for post
git commit

Syntax

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
[--dry-run] [(-c | -C | --fixup | --squash) <commit>]
[-F <file> | -m <msg>] [--reset-author] [--allow-empty]
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
[-i | -o] [-S[<keyid>]] [--] [<file>…​]

git push

Once you’re convinced you to want to update the repository and made all the appropriate commits — git push lets you push the commits to the repository.

Image for post
Image for post
Pushing a commit to the repository

Syntax

git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
[--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
[-u | --set-upstream] [-o <string> | --push-option=<string>]
[--[no-]signed|--signed=(true|false|if-asked)]
[--force-with-lease[=<refname>[:<expect>]]]
[--no-verify] [<repository> [<refspec>…​]]

git status

git status compares the current working head with the latest commit and displays the changed files.

Image for post
Image for post
git status

Syntax

git status [<options>…​] [--] [<pathspec>…​]

git diff

Similar to git status — the git diff gives you a clear idea of which files have been changed. It compares active changes with the previous commit.

Image for post
Image for post
git diff

Syntax

git diff [<options>] [<commit>] [--] [<path>…​]

git checkout

Let’s say you’re working on a new feature and suddenly you get an emergency call from your client. The client found a critical bug and wants you to fix it as soon as possible. Is there a way to save your changes without pushing the unfinished code to production? Fortunately yes! It’s called branching in git.

Image for post
Image for post
git checkout

Syntax

git checkout [-q] [-f] [-m] [<branch>]

git merge

Now that we fixed the critical bug, the question is how do we get our code on the master branch? We made our critical changes on the my-criticl-bug-fix-branch and now we can use the git merge command to merge the changes into the master branch.

Image for post
Image for post
git merge
Image for post
Image for post
git branch -d

syntax

git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
[--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
[--[no-]allow-unrelated-histories]
[--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>…​]

Conclusion

Thanks for reading, I hope I managed to nudge you in the right direction when it comes to using git. Happy coding!

Written by

Software engineer, tech journalist, startups. Stay up to date https://thewholesome.dev, www.startupscrushing.com Twitter @ https://twitter.com/lasnindrek

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store