DEV Community

Der Sascha
Der Sascha

Posted on • Originally published at blog.bajonczak.com on

Get your Git Knowledge to the next level

Get your Git Knowledge to the next level

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
Enter fullscreen mode Exit fullscreen mode

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

git checkout sourcebranch -- c:\path\to\file.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will result the output like this:

Get your Git Knowledge to the next level

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

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
Enter fullscreen mode Exit fullscreen mode

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

Get your Git Knowledge to the next level

Now it's time to rename the commit with

git commit --amend
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)