In case you don't want that to happen, but rather you want Git to really perform a merge, you could either change Git's configuration, or run the merge command with the -no-ff flag.įirst, undo the last commit: git reset -hard HEAD~1 Actually, Git did something very simple – it reset the main branch to point to the same commit as the branch new_feature. So what happened here? Consider the history: The result of a fast-forward merge (Source: Brief)Įven though you used git merge, there was no actual merging here. Since new_feature never really diverged from main, Git could just perform a fast-forward merge. This is pretty trivial, as after all, "Commit 3" is an ancestor of "Commit 4".Ĭheck out the main branch (with git checkout main), and perform the merge by using git merge new_feature: Merging new_feature into main (Source: Brief) This means merging "Commit 4" and "Commit 3". Or, in Git's lingo, merge new_feature into main. Time to merge the new feature! That is, merge these two branches, main and new_feature. The branch main is also pointing to "Commit 3". Looking at the history, you have the branch new_feature, now pointing to "Commit 4", which points to its parent, "Commit 3". Next, stage and commit this change: Committing the changes to "Commit 4" (Source: Brief) In this example I will edit an existing file that looks like this before the edit: code.py before editing it (Source: Brief)Īnd I will now edit it to include a new function: Implementing new_feature (Source: Brief)Īnd thankfully, this is not a programming tutorial, so this function is legit □ Now, on the new_feature branch, implement a new feature. If you need a reminder about branches and how they're implemented under the hood, please check out a previous post on the subject. You can look at the outcome by using git log: The output of git log after using git checkout new_feature (Source: Brief)Īs a reminder, you could also write git checkout -b new_feature, which would both create a new branch and change HEAD to point to this new branch. Next, create a feature branch by typing git branch new_feature: Creating a new branch with git branch (Source: Brief)Īnd switch HEAD to point to this new branch, by using git checkout new_feature. OK, so let's say I have this simple repository here, with a branch called main, and a few commits with the commit messages of "Commit 1", "Commit 2" and "Commit 3": A simple repository with three commits (Source: Brief) Thus we say we "merge branches" – though under the hood, we actually merge commits. Almost always, we merge two commits by referring to them with the branch names that point to them. When you merge with Git, you merge commits. So if you start from the pointer of feature_1, you get to ba0d2, which then points to 54a9d. Notice that commit 54a9d is also on this branch, as it is the parent commit of ba0d2. Of course, as in other posts, I only write the first 5 digits of the SHA-1 value. You see the branch feature_1, which points to a commit with the SHA-1 value of ba0d2. That is, if you consider this commit graph: Commit graph with two pointers (Source: Brief) When we think about commits as being "on" a specific branch, they are actually reachable through the parent chain from the commit that the branch is pointing to. Remember that in Git, a branch is just a name pointing to a single commit. In a way, merging is the complement of branching in version control: a branch allows you to work simultaneously with others on a particular set of files, whereas a merge allows you to later combine separate work on branches that diverged from a common ancestor commit. Merging is the process of combining the recent changes from several branches into a single new commit that will be on all those branches. How to Use VS Code to Resolve Conflicts.I am working on a book about Git! Are you interested in reading the initial versions and providing feedback? Send me an email: are you ready?.If you wish to watch alongside reading, you can find them here ( Part 1, Part 2). I also created two videos covering the contents of this post. By reading this post, you are going to really understand git merge, one of the most common operations you'll perform in your Git repositories.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |