Which means tht our HEAD will point to the new location. The above command will roll us back one commit backwards (HEAD^) or to any indicated commit. We can use the Git reset command for this: Let’s imagine a situation where, after executing one or more commits, we find that we want to roll back to the previous version and continue working from a new starting point. However, we must remember that this leads to a high risk of losing some of our commits. This is not a wrong action, sometimes it is expected and we intentionally take such a step. In short, it is a state in which our HEAD does not point to any existing branch, but directly to a given commit. And what does it give us? Let’s further explain the state we call “detached HEAD”. Well, we have already cleared out what a branch is, how it works and what HEAD is. There we will find one line that can tell us which branch (or commit) the HEAD is currently pointing to, for example something like this: Or execute a simple command that will show us its contents: git directory in our project and find the file named HEAD there. But there is another way, which does not require our observation skills. We can do it in many ways, for example, modern IDEs always mark a branch in a graphical way, in the CLI we can display our Git status and also graphically see where this special indicator is. Well, let’s go further and explain how to check what HEAD is pointing to in our project. So, Git branch is a pointer to a commit, and HEAD is a pointer to a branch, that’s how we can think of it. Though, remember that there is only one HEAD indicator in the whole project! The one you are currently on and what version of the project you see on the screen. We can even have several branches pointing to the same commit! Why not? By the way, it’s quite common.Īnd what is the HEAD itself? It’s a special indicator that shows the current branch. This allows us to add or remove branches according to our desire. Changing of this pointer is very fast, because it doesn’t reload our entire project, but only jumps to another commit. Thus, the branch itself is just a simple little file, containing the SHA code of a particular commit. They are permanent, immutable, and create a linear history of the project, so we can roll back to any version of the file from the past.Īnd what does a branch have to do with it? This is where the cleverness of this solution reveals itself, because a branch simply points to a specific commit, for example, the latest commit on the main branch. The commits are the ones that contain any new, deleted or edited files. Being even more precise, it is not the branch that holds such information, but the commits. Instead, Git tracks a reference to the previous version. If we change only one file in the project then the others are not saved again. Branch in Git does not track the entire version of the project, but only individual changes. The solution to this problem in Git deserves applause. This is a vital thing in modern projects. Among other things, Git has gained immense popularity because it allows users to switch between different versions of a project very easily and quickly. How does it work? The ability to branch a project is crucial to modern Version Control Systems. Let’s first briefly discuss what a branch is in Git. And that’s what this material is about – how managing Git commands we can get rid of the results of our work by carelessly using something called a HEAD. It’s not the tool that can spoil the results of our work, but we are, when we use them in a wrong way. In the IT world, we use a variety of tools for our daily work, so let’s take this lesson to ourselves. Let’s not blame the tools, but rather look at ourselves to see if we are using them correctly! We are the ones who decide for which purposes to use a hammer – to break a window or put a nail into the wall. Do you agree with this suggestion, don’t you? After all, a tool only has certain characteristics and functionalities, but we are the ones who use it in some way or another. We benefit from them in a number of situations, but sometimes they still can cause some problems. There is a great variety of DevOps tools that serve different purposes and uses.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |