Learn some common concepts and techniques used in Git (Part 2)

Tram Ho


Today I will learn about some of the commonly used techniques when we use Git in the process.

Some techniques are often used in Git

1. git stash

While you are working, there will be times when we are bad at branch A and there is a need to fix something on branch B, if you want to switch to branch B, Git requires you to commit the unfinished things, or you want to pull code others have pushed up to the current branch you are working on, but you cannot pull because Git also requires you to commit what you are not doing.

The problem here is that we want to solve the problem we raised, but we don’t want to generate an excess commit, we just want to save the unfinished state and commit only when necessary. Git stash will help us solve this problem, this command will help you save the current state to switch to another branch, or pull code and then continue the current work.

Here is a list of commands with features related to the git stash concept:

Below I will present the simplest and most concise way to solve the problem set out above, I will talk step by step and explain the general intent:

  1. git status (see what files I’m working on (sometimes I skip this step))
  2. git stash (save status on working files) -> git stash = git stash save. After that, you try git status again to see that the working files have returned to the state before you edited (on the most recent commit).
  3. git stash apply . This means that after switching branches, or pull code, … done and next you want to undo the previously saved changes, use the command git stash apply => this command will get back the saved changes Last.

So we have solved the problem mentioned with git stash, but in addition to the most basic way of saving the current state as we presented, we can completely create a status list. status to save not only the current state and also can clear the list of saved states. You can learn more to understand the details of the commands related to git stash I listed above.

2. Reset branch in local just like branch in remote

To reset your local branch to the same state as branch_name on remote, do the following:

git fetch origin

git reset --hard origin/branch_name (repeat origin here is the name of the remote repository and at the same time most remote repository will be named origin)

3. Delete branch

Delete branch at local $ git branch -d branch_name

$ git branch -D branch_name

The -d option is an alias for –delete , with this option you can only delete branches that have been merged with other branches, if not merged, you will get an error message like this:

And the -D option is the alias of –delete –force , it will allow you to delete the branch to be deleted without regard to its merged state.

Delete branch at remote To delete branch at remote, we can use the following commands $ git push -d <remote_name> <branch_name>

Or maybe with the command $ git push <remote_name> :<branch_name>


3. Cancel any edits currently made to the file / folder

This feature is usually used to return files / folders to their original state, we can use the following commands:

Applying to a few specified files, we use the following command:

$ git checkout path/filename1 path/filename2

Or apply to the entire file being edited, we use the following command:

$ git checkout .

4. Reset the file to modified status (before the git add command) after adding

There will be times when you need to use this feature for some reason.

For example, if you have 2 files editing but this commit you need to add only 1 of them but you have already added both files, now we use this command:

$ git reset path/to/file will cancel the added state of the file

Or maybe you want to bring all the added files back to modified state before adding, we will use the following command:

$ git reset

5. Delete untracked files or folders (new files / folders and files automatically created by the IDE)

$ git clean -f or $ git clean -fd

6. Change the content of the newly committed message

When the content of your commit contains bad words due to pressing =)), or misspelling or for some reason you want to edit the commit content, … you absolutely have Can do this easily (of course, only committed but not pushed). This feature is very simple, we will do with the following command:

7. Commit mistakenly to another branch

There are some of you (sometimes me too =))) often choose to develop the feature in the branch develop or even in the master branch, after doing so, create a valid branch and checkout to the correct branch. . But sometimes life is not as we planned, done then we forget and commit always on the current branch =)). I will handle this case as follows:

8. Go back to the previous commit and keep the edited files

Assuming you’re on branch A with the latest commit, you want to go back to the previous commit and return the files on the current commit to modified state $ git reset <commit hash>

See an example. This is the current log

Use the command: $ git reset 609aa2b and then check the log again, we will see the result that commit 5 will be no more but the files edited on commit 5 will not be reverted but will be in the modified state as before. when making commit 5 above

9. Revert a commit

To do this, we can use the following commands: $ git revert <commit hash>

This command will create a new commit following the commit history and reverse the changes in the specified commit. Also, assuming the specified commit is made before a number of subsequent commits, the subsequent commits remain unchanged. A little confusing, right? =)) Give me examples for visualization:

For example:

First check the log: git log --oneline --graph and see the history commit, we see the following:

We will then revert the commit with msg as “commit 2” with the hash of “1bdc17f”: $ git revert 1bdc17f

This will generate a “revert commit 2” commit, we check the log again and see if the result is correct:

Obviously we see the commit is created will follow the timeline (after commit 4 ) and also what you do in the commit 2 will revert back completely, and what code in commit 3, commit 3 remains has changed. Ok just come here, hope you understand my explanation =))

NOTE and WARNING ⚠️ : This is just a small example of the khái niệm revert in Git, but also a lot of techniques will come with the ability to “improvise improvisation” related to this concept. In my opinion this is a technique that requires you in addition to having a relatively good understanding of Git (to be able to flexibly handle problems if they arise after reverting), and at the same time you need to I have a thorough understanding of the project I’m working on =)) I say that because using this feature is often very prone to conflict, if it is less likely to be a problem but there are too many conflicts, the possibility is also painful. very good =))


In this article, I have listed a number of Git techniques that come with the situations that are posed, we can say that we will encounter a lot in the process of working. In addition, there are many other techniques that we will often encounter and have to apply but because I do not have much time for this article, I have not listed it yet and I will add it later. This article presents some fairly basic techniques, but there are also some that can be considered a bit difficult for those new to Git. In addition, this article has outlined some concepts but only stopped at the most basic and simple instructions, because some concepts have a lot of techniques related to that concept and for each person. there will be different ways to apply and I can not afford to go into those concepts thoroughly =)), hope you understand. Thank you for reading!

Share the news now

Source : Viblo