DEV Community

loading...
Cover image for 20 Git Commands You should know ๐Ÿ˜Ž

20 Git Commands You should know ๐Ÿ˜Ž

1nj3ct0r profile image 1nj3ct0r ใƒป3 min read

Git is a version control system developed in 2005 by Linus Torvalds (The creator of the Linux kernel) ๐Ÿ˜ It helps you keep track of the code changes you have made to files in your project ๐Ÿ˜ It comes with a large number of commands that you can use to manage your source code efficiently

In this article ๐Ÿ“œ we'll go over the 20 most commonly used Git commands that every software developer should know

Checking the Git Configuration ๐Ÿ”ง

git config -l
Enter fullscreen mode Exit fullscreen mode

The above command displays a list of information about your Git configuration ๐Ÿค” including username, email, default code editor, etc.

Configure your Git Username ๐Ÿ™โ€โ™‚๏ธ๐Ÿ™

git config --global user.email "example@sample.com"
Enter fullscreen mode Exit fullscreen mode

The above command could be used to configure your email address ๐Ÿ“ง Replace your email address with example@sample.com

Initialize a Git Repository ๐Ÿ˜€

git init
Enter fullscreen mode Exit fullscreen mode

The above command can be used to initialize ๐Ÿ˜ƒ That is to create a new Git repository ๐Ÿ˜„ It can be used to convert an existing project to a Git repository ๐Ÿคจ The above command creates a new .git subfolder in your current working directory that contains all the require metadata for the new repository

Adding a single file to the staging area โž•

git add FILE
Enter fullscreen mode Exit fullscreen mode

The above command adds a file to the staging area ๐Ÿ“ Be sure to replace FILE with the name of the file to be added to the staging area

Adding all files to the staging area ๐Ÿ—ƒ๏ธ

git add .
Enter fullscreen mode Exit fullscreen mode

The above command adds all files to the staging area

Check Git Status ๐Ÿ“‹

git status
Enter fullscreen mode Exit fullscreen mode

The above commands displays the status of the current repository including the current branch, the list of deployed, undeployed, and untracked files, etc.

Maintain Changes ๐Ÿ™‚

git commit
Enter fullscreen mode Exit fullscreen mode

The above command commits the changes to head ๐Ÿ™ƒ When executed it opens a code editor in the terminal where you can write a commit message

Fix Changes with a Message โœ‰๏ธ

git commit -m "YOUR COMMIT MESSAGE"
Enter fullscreen mode Exit fullscreen mode

This command lets you specify just a short summary for your commit message without opening the code editor โœ๏ธ Replace "YOUR COMMIT MESSAGE" with your own commit summary which describes the changes in your commit

Check Git History ๐Ÿ•

git log
Enter fullscreen mode Exit fullscreen mode

The above command displays a list of commit logs

Get Branch List Use ๐Ÿ“

git branch
Enter fullscreen mode Exit fullscreen mode

The above command to display the list of all created branches in the local repository

Delete a Branch ๐Ÿ—‘๏ธ

git branch -d BRANCH
Enter fullscreen mode Exit fullscreen mode

Use the above command to delete a Git branch โž– Make sure to replace BRANCH with the name of your own branch ๐Ÿ™‚๐Ÿ™ƒ Also don't forget to add the -d flag ๐Ÿšฉ It tells Git that you want to delete the specified branch

Create a New Branch โœ”๏ธ

git branch BRANCH
Enter fullscreen mode Exit fullscreen mode

Use the above command to create a new branch ๐Ÿ˜‰ One thing we need to keep in mind is that Git does not automatically switch to this branch - you need to do this manually with the checkout command (See #14)

Change Branches ๐Ÿ”„

git checkout BRANCH
Enter fullscreen mode Exit fullscreen mode

You can use the above command to switch to a newly created branch or to another branch

Create a new branch in Git and switch to it immediately ๐Ÿ”ƒ

git checkout -b BRANCH
Enter fullscreen mode Exit fullscreen mode

You can create and checkout a new Git branch in a single command by adding the -b option to the checkout command

Adding a Remote Repository in Git โฌ†๏ธ

git add remote "https://REPO_URL"
Enter fullscreen mode Exit fullscreen mode

The command adds a remote repository to your local repository ๐Ÿ˜Š Make sure to replace REPO_URL with the actual URL of the remote and repository

Commit your changes to a remote repository in Git ๐Ÿ˜‡

git push
Enter fullscreen mode Exit fullscreen mode

You can use the above command to commit your changes to the remote repository

Pulling Changes from a Remote Repository in Git ๐Ÿคฉ

git pull
Enter fullscreen mode Exit fullscreen mode

You can use the above command to pull the latest changes from the remote repository

Stowing Changes ๐Ÿ˜œ

git stash
Enter fullscreen mode Exit fullscreen mode

The stash command allows you to temporarily park (stash) your uncommitted changes (Both staged and unstaged) to save them for later use

Undoing Saved Changes ๐Ÿ˜Ž

git stash pop
Enter fullscreen mode Exit fullscreen mode

Use the above command to reapply changes parked with the stash command

And that's it ๐Ÿค“ Those were the 20 Git commands I use most often
I hope you found this article useful - have fun ๐Ÿ˜…๐Ÿ‘‹

Also Published on C Sharp Corner ๐Ÿ”—

Discussion (1)

pic
Editor guide
Collapse
omrisama profile image
Omri Gabay

git pull --rebase should be here, if we count specific arguments as different commands