The Advanced Git Concepts

The Advanced Git Concepts

ยท

7 min read

Many people delay learning Git instead, they focus on trending technologies. Git is one of the essential tools that every company used. If you are not familiar with Git basics then I have an article that you can refer to learn the basics of Git.

Table of Content

  1. Git checkout

  2. Amending Commits

  3. Stashing

  4. Git restore

  5. Git reset

  6. Git revert

1. Git checkout

Git checkout command is used to switch between branches and discarding changes in the file. It's similar to the git restore and git revert command, which we will see later on in this article.

Creating and switching between branches

We can create a ** branch** using the git checkout command. To create a new branch and switch to it, we use the below command-

git branch <branch_name> 
git checkout <branch_name>

Alternate command to create and switch to the branch instantly we can use below command-

git checkout -b <branch_name>

Undoing Changes

Suppose you are working on some project and you made some changes to a file but you don't want to keep them. and you want to revert back to whatever it looked like when you last committed, then you can use the below command:

git checkout HEAD <file_name>

2. Amending Commits

Suppose you are working on a project and you just made a commit and then realized you forgot to add some files or you made some typo inside the commit message from this situation --amend option will save you. instead of making a separate commit, you can redo the changes using the git commit --amend command.

Remember it will not work if you made a mistake 10 commits ago. It will only work when you made a mistake 1 commit ago.

suppose I am working on a website and I am writing my code inside git-blog directory and I have added two files index.html and style.css you can see below-

Screenshot (107).png

and I added only index.html in staging area and committed the changes. later I realized that I forgot to stage the style.css. In this situation --amend option will help me.

First, you have to stage the forgotten file using git add forgotten_file then use git commit --amend this will open a file in your configured code editor. In my case, I have configured visual studio code to do this open git bash and run below command-

#configuration for visual studio code
git config --global core.editor "code --wait"
#configuration for atom
git config --global core.editor "atom --wait"

Screenshot (108).png

Screenshot (115).png

When the file opens in your configured code editor do the changes and save the changes and just close the file.

3. Stashing

The first question is why do we need to use git stash command. suppose you are working on a project and you created a new branch inside a project and made changes but due to some reasons, you want to switch to the master branch without committing changes to the newly created branch. In this case, you can use git stash to save the changes and you can return to them later, without having to make unnecessary commits.

I am working inside a directory called git-blog and I am on the master branch you can see the content below-

Screenshot (110).png

After In the same directory, I created a branch called custom and changed some content you can see below-

Screenshot (111).png

Screenshot (112).png

Now due to some reasons, I have to go back to the master branch but I don't want to commit the changes of the custom branch In this situation, I will use git stash command.

Git stash

Git stash is a super useful command that helps you to save changes that you are not yet ready to commit. you can stash the changes and then come back to them later.

git stash

An alternative of this command-

git stash save

Screenshot (113).png

Git stash pop command

Use git stash pop to remove the most recently stashed changes in your stash and re-apply them to your working copy.

Screenshot (114).png

Git stash apply

You can use git stash apply to apply whatever is stashed away, without removing it from the stash.

git stash apply

Git stash drop

To delete a particular stash you can use, git stash drop <stash_id>.

# for example I want to drop stash having stash_id(stash@{0}) then I will use
git stash drop stash@{0}

4. Git restore

Suppose you are working on a website and added code for the footer. later you made more changes in the footer but after some time you realized that you don't want those changes then you can use git restore which helps you to undo changes. Git restore is a brand new command most of the existing Git tutorials and books do not mention it, but it's worth knowing.

As you can see in the previous index.html file, I don't have any other content except the h1. then I added some random text you can see below but I don't want these changes so I used the git restore command to undo changes I made.

git restore <file_name>

WARNING! Things to remember before running this command If you have uncommitted changes in the file, they will be lost. git restore command is not undoable.

Screenshot (116).png

Screenshot (117).png

** What is --source?**

Git restore uses HEAD as the default source, but we can change that using the --source option.

For example, git restore --source HEAD~3 nav.js will restore the content of nav.js to its state from the commit you specified after the HEAD. we can also use a particular commit hash as the source. In HEAD~3, ~3 means 3 commits ahead of HEAD.

To get commit hash you can use the below command-

git log --oneline

Screenshot (118).png

Unstaging files with restore

Suppose you are working on some projects and accidentally you added a file in your staging area with git add <file_name> but you don't want to include that in your next commit. To overcome this problem you can use the below command-

git restore --staged <file_name>

5. Git reset

We use git reset when we want to undo some commits. Let's understand this, assume you are working on some branch and made multiple commits on that branch. but later you don't want those commits on your working branch, you want those commits on a separate branch. then you can use the git reset <commit_hash> command.

git reset <commit_hash> command only removes the commit hash. It won't delete the changes in your local machine.

I made multiple commits on the master branch you can see below-

Screenshot (119).png

But I want to reset to this 22e88fa commit hash for this I will use the following command

git reset 22e88fa

Screenshot (120).png

As you can see previously my HEAD was pointing to the fb41f33 commit hash now it's pointing to the 22e88fa commit hash which I specified in the above reset command.

--hard option

Use the --hard option If you want to undo commits and the actual changes in your files.

git reset --hard <commit_hash>

6. Git revert

The git revert can be considered as an undo type of command. and It is similar to other undo commands like git checkout and git reset. we saw git reset and git checkout move the HEAD and branch ref pointers to a specific commit.

How does Git revert differ from Git reset

Git revert and reset both do the same thing undoing.git reset move the HEAD and branch ref pointers to a specific commit. git revert also takes specific commit, However git revert does not move ref pointers to this commit. It will take a specific commit and creates a new commit. The ref pointers are then updated to the newly created commit.

Let's understand this by doing. I have one commit called Random commit, HEAD ref pointing to this commit you can see below. and I am going to apply the revert command on this commit

Screenshot (121).png

It will open a file in your configured editor. for mine, it's visual studio code. you can see pointer ref is not pointing to Random commit, now it's pointing to newly created commit hash.

Screenshot (123).png

Screenshot (122).png

Which one Should I use git revert or git reset?

Both git revert and git reset help us reverse changes, but there is a significant difference when it comes to collaboration.

If you want to reverse some commits that other people already have on their machines, you should use revert.

If you want to reverse commits that you haven't shared with others, use reset and no one will ever know!.

References: Colt Steele

Thank you for reading. ๐Ÿ™

If you enjoyed this article or found it helpful, give it a thumbs-up. ๐Ÿ‘

Let's Connect. ๐Ÿ‘‹

Github | LinkedIn | Twitter

ย