Git

5 Useful Git Commands You May Not Know

5 Useful Git Commands You May Not Know

Even though there are a lot of Git GUI clients out there one must know how to work around git using commands. The main reason being different teams uses different Git GUI tool and it hinders your productivity when you get into a new project.

Here I’m covering some of the git commands which will definitely help you if you are working with a team.

Table of Contents
1. How to undo your commit from a local branch?
2. How to show only your commit or a specific author’s commit?
3. Show the files in the commit log.
4. Change your last commit message from the local branch.
5. Set different username and email for different projects on your computer.

1. How to undo your commit from local branch?

While working for a long time on big projects there’s a big chance of missing out something while committing your changes or even discard it completely.

In these cases git reset comes for your rescue.

1. Undo your last commit and keep the changes.

git reset --soft HEAD~1

2. Undo your last commit and discard all changes.

git reset --hard HEAD~1

Make sure you use --hard flag only if you are sure you don’t want to preserve these changes

3. Undo multiple commits.

This will undo all the commit till the one which you have specified. If you are not sure, don’t try it on your main project.

git reset --soft b1da77b

where b1da77b is the commit hash.

Note that this will mess up things if you have already pushed your codes to your remote repository.

2. How to show only your commit or a specific author’s commit?

This comes in handy if there are a lot of people working on your project and you want to see the commits made just by you or by any other developer.

git log --author="Athul"

You don’t have to enter the full name of the author

In case you want to see the commits by an author in all branches you can add --all option

git log --author="Athul" --all

You can combine this command with the below one to get more insightful result.

3. Show the files in the commit log.

Ever wanted to see which all files you have modified in a project? This command comes in handy if someone in your team asks you to provide a list of files you have worked on in the project.

git log --name-status

The above command will display files changed along with the commit log. You can use the below options to make it more useful

OptionsDescription
--name-onlyDisplay only the file name (Added/Modified/Deleted status won’t be shown)
--onelineFor short and compact result (won’t display author or date and time)
--diff-filter=ADisplay only the files which are Added (M for modified and D for Deleted files)

Good? Okay, for instance, let’s say I want to see all the files I have modified in the current branch.

Then I can use the below command

git log --name-status --oneline --dif-filter=A --author=Athul

4. Change your last commit message from the local branch.

A couple of times I have made mistakes in my commit message. Like missing out your Jira or Github ticket number.

In such cases, we can amend the last commit using the below command

git commit --amend -m "Correct commit message"

However, it only works with the last commit. If you notice your mistake after adding another commit this won’t work.

Amend replaces the old commit by a completely new one (a new and different commit object). This makes it very important that you don’t amend or rewrite commits that you’ve already published to a remote repository! Because it might rewrite your teams commit if they have already based their work on this commit.

5. Set different username and email for different projects on your computer.

Most of us use our company username and email in git config. But what if we want to work on over on project. or maybe you have multiple projects which use client-specific email and username. You can use git config --local for fixing this issue.

Go to your local git repository and enter

git config --local user.name="your name"
git config --local user.email="your email"

If your global username and email is overriding your local user data use git commit --amend --reset-author

Conclusion

These are some of the common requirement which comes when you are developing your awesome codebase with your team. A couple of these git commands will definitely come in handy for you.

If you like this post, do up-vote below.


Continue Reading

comments powered by Disqus