DEV Community

Cover image for Git Commands To Know As A Developer [Cheatsheet for Beginners] 👨‍💻📑
Arjun Vijay Prakash
Arjun Vijay Prakash

Posted on • Updated on

Git Commands To Know As A Developer [Cheatsheet for Beginners] 👨‍💻📑

Done with remembering Git commands? Check out this cheat sheet with 10 commands to make your life easier as a code newbie.

You before this post -
ImageYou after this post -
Image

Some Simple Git Terms To Know:

  • Repository: This is where all your project's files, changes, and different versions are stored.

  • Branch: It's like a separate copy of your project where you can work on a specific version. The main branch is often called "master."

  • Commit: Think of it as a single snapshot of the changes you've made to your project in a branch.

  • Checkout: This is how you switch between the current version you're working on and another version in your project.

  • Master: The main version of your project, usually referred to as the "master" branch.

  • Merge: When you combine changes from one branch into another to keep everything up to date.

  • Fork: A clone of a repository that you can work on independently.

  • Head: The most recent snapshot of your project that you're currently working with.

Fundamental Git Commands You Need to Grasp

git init | git init [folder]
Enter fullscreen mode Exit fullscreen mode

To start a new project or set up version control in an existing project, you use this command. It creates a repository either in the current folder or the specified folder.

git clone [repo URL] [folder]
Enter fullscreen mode Exit fullscreen mode

This command copies an existing repository to your computer. If you provide a repo URL, it copies to the current location. If you want it in a different location, include a folder path.

git add [directory | file]
Enter fullscreen mode Exit fullscreen mode

When you want to prepare changes to be saved, this command comes in handy. It stages the changes in a directory or a file. Usually followed by git commit and git push.

git commit -m "[message]"
Enter fullscreen mode Exit fullscreen mode

After staging changes, this command commits them with a message explaining the changes. You can also use -am to add and commit changes together.

git push
Enter fullscreen mode Exit fullscreen mode

To send your committed changes to the main repository, you use this command.

git diff
Enter fullscreen mode Exit fullscreen mode

To see the differences between your current changes and the last committed version, you use this command. You can also compare staged changes with the latest version using -staged, or specify a file to compare.

git pull
Enter fullscreen mode Exit fullscreen mode

This command is used to fetch changes from the original branch and merge them into your local branch.

git fetch
Enter fullscreen mode Exit fullscreen mode

Similar to git pull, this command gets the latest changes from the main branch, but it doesn't automatically merge them.

git log
Enter fullscreen mode Exit fullscreen mode

This shows the history of your commits in a default format, letting you see what changes have been made over time.

git status
Enter fullscreen mode Exit fullscreen mode

This helps you know the status of your files, whether they're ready to be committed, changed, or untracked.

Use this official cheatsheet of Git for more commands.

I hope this article has helped you in learning about Git. Thanks for reading the article! Don't forget to bookmark this post for reference in the future!

Top comments (5)

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman • Edited

To be complete developers should also know about some of the alternative version control systems such as bazaar/bzr which is used on launchpad and subversion/svn which is used on sourceforge.

For new developers it is good to make yourself familiar with these alternatives in the rare instance that you may run across an odd/old project that is still uses them.

Happy Coding

Collapse
 
cmuller profile image
Christophe Muller

To be complete ;-) do not forget Mercurial (mercurial-scm.org/) a free software distributed VCS as well which is fast, elegant and much easier to use than git and though just as powerful. IMHO of course :)

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

HaHaHa thanks for helping me be completer :)

I've never used Mercurial (but I have heard of them) so that's probably why I forgot them. I'm getting older and the memory isn't quiet what it use to be :)

Happy Coding

Collapse
 
beroge profile image
beroge

To clear up confusion for newbies (like me!)

"master" has been renamed to "main" NOT going to get into the reasons why...haha)

Collapse
 
kwamegh22 profile image
KwameGH22

Great summary!!