Photo by Roman Synkevych / Unsplash

Get your Git Knowledge to the next level

Git Sep 24, 2023

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:

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:

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:

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.

Tags