Get your Git Knowledge to the next level
Tags: git
Published: 2023-09-25 06:00:10

---
title: Get your Git Knowledge to the next level
published: true
date: 2023-09-24 13:01:19 UTC
tags: Git
canonical_url: https://blog.bajonczak.com/git-how-to-force-file-replacement/
---

 ![Get your Git Knowledge to the next level](https://images.unsplash.com/photo-1618401471353-b98afee0b2eb?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDF8fGdpdHxlbnwwfHx8fDE2OTU0ODA0MjV8MA&ixlib=rb-4.0.3&q=80&w=2000)

This article will give you some quick commands at your hand, that will give you the power to handle some edge cases.

## Force File Replacement from another branch

Sometimes it will be necessary to replace a file from another branch. So for this, you must switch to the current branch in which you want to replace the file itself

```
git switch targetbranch
```

The you can replace the files in the current branch from the source branch like this

```
git checkout sourcebranch -- c:\path\to\file.txt
```

## Getting a nicer look in you console of the git log

Many use UI Tools because they can see the graphs and branches, but you can do this also in the console by using the following command

```
git log --graph --oneline --branches
```

This will result the output like this:

![Get your Git Knowledge to the next level](https://blog.bajonczak.com/content/images/2023/09/image-3.png)

You can scroll up and down with the arrow keys. Simple huh?

## Reverting to a specific commit

It happens often the you push some commits that you don't want to push. For example let's assume these commits:

![Get your Git Knowledge to the next level](https://blog.bajonczak.com/content/images/2023/09/image-1.png)

In this situation, the publisher pushed two wrong commits (the first two) and wants to revert to the last commit in the screenshot. Also, he want to rename the commit message. For reverting to the specific commit, you must select the hashcode before the commit (d01b57101). After pulling the last version of the brancch, you will be able to revert to this commit with the follwoing command:

```
git reset --hard d01b57101
```

After that, you will see that the first two commits are gone:

![Get your Git Knowledge to the next level](https://blog.bajonczak.com/content/images/2023/09/image-2.png)

Now it's time to rename the commit with

```
git commit --amend
```

This will open up the vi-tool, in this, you can make changes to the commit. You will also get a nice overview of the changes in this commit, to be sure that you get the right commit to change.

After saving the changes, you must publish it to the remote branch for this you must do a force push (this is a special access right, that you must get assigned sometimes).

```
git push --force
```

After that, your remote reflects your local change.

# Conclusion

In conclusion, this article has provided you with a set of quick and handy commands to empower your Git skills, especially when dealing with some challenging scenarios. Whether it's the need to replace a file from another branch, improve the visual representation of your Git logs, or revert to a specific commit while making necessary adjustments, these commands can be valuable tools in your Git toolkit. By mastering these commands, you'll be better equipped to navigate and manipulate your Git repositories efficiently, ensuring smoother version control and collaboration in your development projects.