DEV Community

Cover image for Top 10 Git Commands I Use Daily
Hiba E
Hiba E

Posted on

Top 10 Git Commands I Use Daily

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Git terminal showing status of files


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 .
Enter fullscreen mode Exit fullscreen mode

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

git add folder-name/file-name.file-extension
Enter fullscreen mode Exit fullscreen mode

To stage all changes in all directories:

git add --all
Enter fullscreen mode Exit fullscreen mode

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


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
Enter fullscreen mode Exit fullscreen mode

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

git diff -staged
Enter fullscreen mode Exit fullscreen mode

The output of running the git diff command would be something like this
Git terminal showing output of git diff command


Git Reset

To unstage all files run the below command:

git reset
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
warning: 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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Git terminal showing prompt to push upstream branch

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

    git push --set-upstream origin test-branch
Enter fullscreen mode Exit fullscreen mode

or the shorter version might be easier to remember πŸ˜…

    git push origin test-branch
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

git revert commit_hash
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  1. Pull from remote main branch. Your local branch will be updated with the all the changes from the remote main branch
git pull
Enter fullscreen mode Exit fullscreen mode
  1. Switch to your branch
git checkout my_branch
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
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.

Bonus ⭐️

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)