All Articles

Git Commands

Here are some of the most common git commands I use when working on my personal projects. This post is meant to be updated asmy habits change and I start using new commands.

Git commands

Tell git to stage everything and make it ready to commit

git add .

Commit all the changes

git commit -m "required-message-goes-here"

Tell git to create a new branch from main

git checkout -b branch-name

Merge your newly branch back to main

git checkout main
git merge branch-name

Delete a branch

git branch -d branch-name

Check the status of your git repo

git status

Check your git log

git log

If you have commited your changes and you realize, oh I need to make one small change and that change should ideally go into the same last commit. Like you, I would make another commit and call it a day but that does not make our git log pretty. There is a way to amend a commit.

After your last commit, make your changes and commit again with a special flag seen below

# Make your change after last commit

git add .
git commit --amend --no-edit

The amend flag will add your change to the last commit and the no-edit flag will allow you to not change your commit message. It is important to not change your public commit message, that is after you have pushed your repo. I guess you can change the public commit message but generall it is not good to do so because others would have seen it already.

If you have made a spelling mistake in your last commit message then you can correct that with the following message

git commit --amend

You can find the above commands in this gist.