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.

 

git commit -–amend –m “commit’s comment”

 

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”);

    }

}


Code committed with misspelling

 

But after recheck commitment, you have recognized that you made a mistake when commit code with misspelling. The command –amend will save you.
run commend git commit --amend


By this way, nobody will see your mistake.


git chart flow after run command git commit --amend


2. git reset

This command will reset current HEAD to the specified state


git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

The git flow before run command reset


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

Run command git reset


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

git flow after run command git reset


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:


git stash save

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:


git stash pop

  • 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



Click button OK after inputted name


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


Select "Apply Stash" menu


I hope that these tips will help yourself better with working on git

Cover image from freepik.com