As you know, the basic commands usually using when work in with git: Clone, pull, commit, push … But sometime you will meet some trouble when working git, as commit with wrong message or commit wrong file… How do resolve these trouble? This blog will give you some tips to resolve them.
1. git commit --amend
Using this command when you want to fix comment, add or modify content file for the last commit.
The command will help us correct mistake for the last commit without create a new commit.
Example, we will resolve these trouble below:
-
- Commit with misspelling
- Commit with wrong code
- Commit missing file
You have committed code on git with file’s content and command below:
class TestClass{
static void Main(string[] args)
{
Console.WriteLine(“Hello work”);
}
}


By this way, nobody will see your mistake.

2. git reset
This command will reset current HEAD to the specified state

If you want to change HEAD to “Commit 1” and keep all changes, you can use command

After run this command the git flow will be change be low

3. git stash
While you are implementing current task, the leader requires hot fix on the other branch. How to save all file changes in current branch? You can discard all file changes or commit code on git, but 2 ways above aren’t best way.
You can use command git stash if you want to record the current stage of the working directory. It is very useful when you want to change to another branch but are in the middle of working on the current branch.
You able to record all working in progress, use command below:
After you have completed hot fix, you can return current branch and apply stash. After that you can continue your task.
You can use command below to apply stash in to current work and remove it:
- If you don’t like to make stash by command line, use UI in Sourcetree to make stage easily
To make stash in Sourcetree, click icon Stash in menu icon and input stash name


Right click on stash then select Apply stash to apply this stash in to current work.

I hope that these tips will help yourself better with working on git
Cover image from freepik.com