Skip to main content

Command Palette

Search for a command to run...

Git Cheat Sheet

Updated
7 min read
Git Cheat Sheet
R

I'm a passionate Full Stack JS Developer focused on creating robust and efficient solutions. You'll find articles here covering my experiences with React/Next.js, Node.js/Express, TypeScript, and general development best practices. Let's talk code!

Git :-

Git is a powerful tool that has made collaboration easier for developers. It is used usually for coordinating the collaborative work among developers during software development.

On this article we will be sharing 20 of the most common Git commands with examples so you can get started using Git in your work today.

1.Git init

Use git init to create an empty Git repository.

As long as this command has been run in a directory with nothing else stored inside of it, then that's all you need to do before starting your project and adding files into the new folder using commands like git add.

If there are other projects (or folders) in the same location, those will get cloned by default when running git init.

Command

$ git init <the name of your repository>

2. Git config

To set up Git with your details, you'll need to use git config. In this command, you can specify a username and email address to be used by default whenever new commits are made.

For user name

 $ git config global user.name <name of user>

For user email

$git config global user.email <email address>

3.Git status

The $git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.

Let's try

$ git status

4.Git touch

The touch command's primary function is to modify a timestamp. Commonly, the utility is used for file creation, although this is not its primary function. The terminal program can change the modification and access time for any given file. The touch command creates a file only if the file doesn't already exist.

Command

$ git touch <file name>

Example

$ git touch index.html

5.Git add

The $git add command adds a file to the Git staging area. This area contains a list of all the files you have recently changed. after creating a file in working directory files need to added in staging area. We can add single or multiple files at once in the staging area.

It will be run as:

$ git add <file name>

For adding more then one files

$ git add -A

6.Git commit

The $git commit command is what you'll use to take all of the changes that have been made locally and push them up to a remote repository.

It's important to note that you can't just type "git commit" by itself with no arguments — it needs at least one parameter, which will be either HEAD or any other branch name. Commits are created to capture the current state of a project.

it will be run as

$ git commit //for one file
$ git commit -a -m "comment" // Commit for all modified files

7. Git merge

$git merge is a robust feature that allows you to combine work from two branches into one.

This is useful when developers work on the same code and want to integrate their changes before pushing them up in a branch.

$ git merge <the name of branch>

8. Git diff

Use $git diff to show all the differences between any two commits or files within your Git repository.

It can be beneficial for identifying where there have been conflicts during branching or merging operations.

It will show which each commit/branch added lines and highlight any conflicting lines.

$ git diff <the source branch> <the target branch>

9. Git log

The $git log command lists every commit that has ever happened in your project to see what has changed over time, along with some other information about how the commit was done.

$ git log

10.Git show

Use the $git show command to view the contents of a specific Git object be it a file, directory, or blob.

It helps inspect commits that have been made in your project and preview what changes were applied by each one.

$ git show <commit>

11. Git grep

$git grep searches through your codebases for any occurrences of an expression (i.e., keyword).

This can help you find where something has been referenced without manually searching through all files within your repository that may contain this reference.

$ git grep -n

12. Git branch

Use the $git branch command to create or delete a new branch from an existing one.

$ git branch

13. Git stash

The $git stash command is used to save your changes but not record them in the Git repository.

This can be usefull when you want to stop working on a given feature or section for now and come back later.

$ git stash

14. Git rebase

To update branches with recent commits made elsewhere (for example, if someone else has pushed up new updates while you've been coding), then you'll need to run $git rebase.

$ git rebase master

15. Git clone

$git clone does just what it says - it clones.

it copies your entire Git repositpry so you can work on a copy without impacting the original one, meaning no mearge conflicts or accidental overwrite.

$ git clone <URL of your project>

16. Git checkout

If you want to switch your local branch, then $git checkout is what you'll need.

The name of the current branch will be given as a parameter, followed by another one for which you want to set up.

 $ git checkout <the name of your branch>

17. Git reset

Use $git reset to "un-track" a file to no longer have any links to the Git repository.

This can be useful if you have made changes locally but want those new commits to go up on GitHub or elsewhere for others to see.

 $ git reset <file>

18. Git tag

A $ git tag labels an essential milestone in the project, like when it was published or how old it is. Use tags to mark releases and help you keep track of your history.

$ git tag <commitID>

19. Git archive

To create archives from Git branches, you'll use this command followed by one for which you want to generate the zip file — i.e., master or development.

It will then give you two parameters — name and type — both of which are optional, so don't worry if they're not given as arguments on your first try.

The default values for these are "Zip" (which implies compression) and "Standalone Archive," respectively.

 $ git archive --format=tar HEAD

20. Git rm

Use the $git rm command to remove files from both the working directory and the Git repository.

This means that they'll be deleted on your computer — but also gone for good in terms of being a part of what's saved online.

$ git rm <the name of your file>

21. Git remote

$git remote establishes the connection between your local repository and a remote one.

For this to work, you'll need to know the URL for where it lives — or if you're doing so locally, then just running Git remote is all that's required.

$ git remote add <name of variable> <remote server URL>

22. Git fetch

This command takes any new commits from another branch in an existing Git repository and copies them onto yours by default.

Use this as a shortcut or when too many branches are cloned at once, making things difficult.

$ Git fetch

23. . Git push

To share your local commits by pushing them up to a remote repository, you'll need to run git push within your Git project directory.

This command uploads all changes made locally and sends them up for everyone else on the team (or organization) working on the same codebase.

$ git push -u <name of variable> <name of branch>

24. Git pull

Git pull is what you'll need to fetch the changes from another branch and merge them into yours.

Use this if you're working on a separate but related project or want to get new commits that may have been made in your repository.

$ git pull <repository URL>

*** Thanks for reading ***