Hackernoon logo20 Most Useful Git Commands 😎 by@1nj3ct0r

20 Most Useful Git Commands 😎

1nj3ct0r Hacker Noon profile picture

@1nj3ct0r1nj3ct0r

Hi πŸ‘‹ I'm a 15-year-old geek interested in Systems Programming πŸ’» Web Apps 🌐 and Artificial Intelligence πŸ€–

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

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"

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

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

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 .

The above command adds all files to the staging area

Check Git Status πŸ“‹

git status

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

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"

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

The above command displays a list of commit logs

Get Branch List Use πŸ“

git branch

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

Delete a Branch πŸ—‘οΈ

git branch -d BRANCH

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

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

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

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"

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

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

Pulling Changes from a Remote Repository in Git 🀩

git pull

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

Stowing Changes 😜

git stash

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

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 πŸ”—

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.