Advanced Topics
Working together an merging files/pull requests
Show them how to merge and resolve merge conflicts. There may already be some conflicts from the previous excercises!
Merging
As shown earlier, working collaboratively might require you to separate the tasks and do them in several branches.
However, once task is finialized a merging step into the main branch might be necessary as a final step.
The git merge
coomand allows you to merge a specific branch into the current branch you are on.
Therefore, in case you wish to merge the branch feature into the main branch and currently you are on the feature branch, please follow these steps:
- switch to the main branch first by typing:
git checkout main
- once ready, you can merge your feature branch into the main by typing:
git merge feature
- In case you only worked in separate files that were not modified or created by someone else in the main branch, then the merge will happen conflict free and create a new commit in the history of the main branch.
Otherwise, the merge will be aborted and the conflicted files will be merged and contain the changes of the two versions. However, the conflicted content will be separated by something like this:
<<<<<<<<<<<<<< HEAD
some content modified on the main branch by someone else
===============
your *feature* branch changes on the same content
>>>>>>>>>>>>>>> new_feature_branch
All conflicts must be solved before the branch is merged successfully. For that there are several options:
- either you accept the others’ changes
- force your changes to be chosen
- modify the conflicted files manually and commit the new conflict solution
Rebasing
Similar to merging, rebasing can be used to move the commits and changes from one place to another, either to another branch or can be used in the same branch as well. Rebasing does not create a merge commit, but places the commits of the current branch on top of the HEAD of the branch rebased onto. A common use case is to rebase commits of a local or a feature branch onto the main or another remote branch that has received updates that the feature branch has not yet received in order to preserve a linear commit history.
However, rebasing should only be used with utmost caution as it rewrites the commit history and generates new commit hashes for the moved commits. This can cause conflicts conflicts if multiple work together on the same feature branch.
So use it with caution unless you are working alone in the branch.
- In case used in the same branch, then rebasing can be used to squash the commits together into one in order to shorten the history in case of many minimal commits were created.
- To rebase the current feature branch onto the main, just type
git rebase main
and the feature branch will be entirely rebased onto the main branch’s HEAD. - There is also the option
--interactive
which starts rebasing in interactive mode with several different actions, such as squash specific commits together, picking specific commits to rebase or even drop some of the commits.
More details on this topic can be found here.
Again use git rebase with utmost caution!
git cherrypick
git cherry-pick
is one of the useful and simple commands for git. It applies a specific commit to your current working tree. The commit could be as well in another branch
It requires your working tree to be clean (no modifications from the HEAD commit)
- Example:
-
git cherry-pick commithash
-
git cherry-pick main~4 main~2
-
Apply the changes introduced by the fifth and third last commits pointed to by main and create 2 new commits with these changes.
-
git stash
-
What does the command do?
- Save you current status and revert the directory to
HEAD
- Save you current status and revert the directory to
-
When do I need to use
git stash
?- When you want to record the current state of your directory and branch but want to switch to another branch, or go back to a clean directory
- You don’t want to commit your experimental changes
- Save your un-committed changes in a “stash”
-
Example:
-
git stash
: save your uncommitted changes -
git checkout some_branch
: change to intended branch -
git stash list
lists all you stashes -
git stash apply
apply the chosen stash from the list. E.g.,git stash apply stash@{1}
-
Logging and Mistakes
-
Most common mistake: I committed something to main but it should not have been there. There are two options to revert or delete a commit:
-
git revert
: creates a new commit that reverts the changes made in the previous commit. -
git reset HEAD~ --hard
: removes the last commit from the main branch. Note that using the--hard
option will also delete any uncommitted local changes.
-
-
You did something wrong, accidentally deleted or just want to remove stuff you tried that broke the repo
-
git reflog
: you will see all changes you did, even across branches -
Find the one before you broke everything
-
git reset HEAD@{index}
-