Top 10 Git Commands I Use Daily
Tags: git, webdev, programming
Published: 2022-01-31 00:01:23

I've been using Git for a few years now but every now and then I still find myself Googling the commands that I need to use for different cases and scenarios. Below is a list of the top 10 commands I use every day without fail.


## **Git Checkout**
Git repositories use branching strategies to create separate versions of a main repository. So to get started on making changes to your codebase you need to do one of two things:
- Checkout an existing branch 
- Checkout a new branch

###### _A Git branch is a separate version of the main Git repository (A master or main branch)._

Whichever option you use you'll need to use the git checkout command.
- To checkout a **new branch** you would need to run the below command:

```
git checkout -b my_branch_name
``` 

The above command creates a separate local version of the repository using the branch you were on when you ran the command. 

- To checkout to an **existing branch** all you need to do is remove the -b parameter:

```
git checkout branch_name
```
---

## **Git Status**

Once you're all set and making changes to your solution, you can check the status of your code against your local copy by running the below command:

```
git status
```

This returns the status of files in your solution (files added, updated or deleted).

![Git terminal showing status of files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efj2ri6i1vzwpvdztvhv.png)

--- 

## **Git Add**

Once you're happy with your changes you'll need to stage the files. This is a way of letting Git know which files you want to record (i.e. commit) changes to. By running the below command you're telling Git to record/stage all changes made in your current directory:

```
git add .
```

To stage certain files copy the relative path of the file and run the below command:

```
git add folder-name/file-name.file-extension
```

To stage all changes in all directories:

```
git add --all
```

Once you run the above command, your files will show as "staged" when running the `git status` command again 👇🏼

![Git terminal showing status of files after staging a file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/666v7uwdazefu88x6pbb.png)

---

## **Git Diff**

Once you've made changes, you can check the changes you've made so far for unstaged files using the below command:

```
git diff
```

To check file differences that are staged run the below command:

```
git diff -staged
```

The output of running the git diff command would be something like this
![Git terminal showing output of git diff command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cshrgjeq0qccolro844c.png)
 
---

## **Git Reset**

To unstage all files run the below command:
```
git reset
```

To unstage specific files files copy the relative path of the file and run the below command:

```
git reset folder-name/file-name.file-extension
```

To discard all your local changes that haven't been committed yet, you can run the below command that will undo all your changes **permanently**

```
git reset --hard
```

If you've made a mistake with your last commit to your _local_ branch, not to worry it's easily fixable.

If you've not pushed your changes to your remote branch, you can just run the below command: 

```
git reset --soft HEAD~1
```

The above command will **_remove_** the last commit from the current branch and move the changes to your pending changes so you'll be able to commit/amend them again as needed. 

If you don't want the changes to show as pending on your local branch either you can run the below command:

```
git reset --hard HEAD~1
```
	

##### **<u>warning</u>: by running this command you will lose all uncommitted changes and all untracked files in addition to the changes introduced in the last commit**
---

## **Git Commit**

Now that you're done with all your changes and ready to commit the files to your local machine, you can run the following command with a relevant commit message:

```
git commit -m "This is the commit message"
```

The above command will commit your changes to your local Git repository so you will no longer see pending changes when running Git status. Add your commit message after the -m parameter. It's good practice to write concise, unambiguous and informative commit messages. An example of a good commit message is: "Add component for wishlist button in the website header"

Looking back at this commit, we'll have no issues understanding what this commit does.

#####Note: Running git commit without the -m parameter would open up your default text editor to write a commit message.
---
 
Using git commit with the **amend** parameter you can also rewrite the last commit you made. The amend parameter allows you to: 

1- Update/rewrite the changes in the last commit. So any newly staged changes will be combined with the changes in the last commit to your branch. To add changes to the last commit _without_ updating the commit message run the following:

```
git commit --amend --no-edit
```

2- Update/rewrite the changes in the last commit _and_ to update the commit message:

```
git commit --amend -m "This is my updated commit message"
```

3- Rewrite the commit message of your last commit. To do this run same command as number 2 (_make sure you don't have any files staged if you don't want to commit them_)👆🏼
```
git commit --amend -m "This is my updated commit message"
```

This is particularly useful in the case of premature commits, if you miss a file from a commit or if your commit message isn't quite right. A new commit would then be created replacing the old commit. 

####Note: this should only be done on _local_ commits that haven't been pushed to the remote branch unless you're _completely_ confident no one is using the same branch. This is to avoid issues that will arise if other developers are using the same branch and have based their work on a commit that is amended after your changes. 

---

## **Git Push**

After running the Git commit command, your changes are now in your local repository. Once you're 100% happy with your changes and want to push them to the remote Git repository you need to run the below command:

```
git push
```
	
If your branch does not have an associated remote branch, you'll get the following prompt:

![Git terminal showing prompt to push upstream branch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bk7ncown11zo02dkojua.png)

To resolve this, you would need to run the below command:

```
    git push --set-upstream origin test-branch
```
 
or the shorter version might be easier to remember 😅

```
    git push origin test-branch
```

All your previously pending changes would now be available on the remote Git repository. You'll now be able to see your changes showing in the Git repository on the server and not just on your machine! 🎉

---

## **Git Pull**

Git pull downloads changes from the remote branch on the server and merges those changes into the local branch you have checked out. This would only work if the branch you have checked out is tracking an upstream remote branch.

By running the git pull command, the remote tracking branches for all other branches will also be updated.

The way this is done is by running the `git fetch` then `git merge` commands in the background.

```
git pull
```
---

## **Git Revert**

If you've made a mistake with your last commit and you **_have_** pushed your changes to the remote branch, You'll need to use `git revert` and specify the commit hash you want to revert. You can get the commit hash by running `git log`

```
git log --oneline

```

Once you have the commit hash, you can revert your commit by running:

```
git revert commit_hash
```
#### Comparison between Git reset and Git revert

`git reset` _discards_ the commit on your local branch so you'll no longer see you in your list of commits, while `git revert` _creates_ another commit to revert the changes made in the commit you need reverting. 

---

## **Git Log**

- If you want to see a list of previous commits  you can check the Git log ! Just run the below command: 

```
git log
```

- If all you want is a list of commits with the commit hash and message only you can run the below command:

```
git log --oneline
```

- Once you've found a commit in the log that you want to find out more about, copy the commit hash from the previous command and run the following command `git show commit_hash`. This will show you all the changes made as part of that commit. To can exit the commit window just press :wq and enter

---

## **Git Merge**

Let's say you need to merge from the main branch to your new branch, to make sure you have the latest changes, you can do that by following the steps below: 

1. Switch to the main branch/version of your Git repository
 
```
git checkout main
```
2. Pull from remote main branch. Your local branch will be updated with the all the changes from the remote main branch
```
git pull
```
3. Switch to your branch
```
git checkout my_branch
```
4. Merge from the main branch to your branch. The following command will copy over the code from the main branch and merge it with the code in your branch. 

```
git merge main
```

#####You may see conflicts resulting from this which would need to be resolved. The most direct way to resolve a merge conflict is to edit the conflicted file. 

---

##<u>Bonus</u> ⭐️
## **Deleting a branch** 

This is one that I always seem to forget and always end up Googling so I thought I would add it here 😁 
- Once you're done with a branch, to delete branch on your local machine all you need to do is run the below command:
```
git branch -d branch_name
```

- If you have a remote version of your branch on origin, you can run the below command to delete it too:

```
git push origin --delete branch_name
```
---

##### So these are my top 11 most-used Git commands that I come across daily. There's a lot more to Git than the commands listed above and I hope to be able to explain a few more Git commands in future articles.

##### Thank you for reading. I hope you found this useful 😃 Let me know what other Git commands you use on a daily basis. If you have any comments, questions or feedback, please feel free to leave a comment below or reach out to me on [Twitter](https://twitter.com/dev_hiba)

---