Special Topics
Contents
Special 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_branchall 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. The key difference here is that it does not necessarily create a new commit after rebasing the feature branch into the main.
However, it used as an alternative to merging, then only with outmost careful specially if many are working in the to be rebased branch since after rebasing all the commits will be moved and a new commit hash will be generated for each one of them. Thus, resulting a conflict chaos with the working colleagues on the same 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 into the main, just type git rebase main and the feature branch will be entirely rebased to 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.
In case of interest for more details look here.
Note
Again use git rebase with outmost 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 branchIt requires your working tree to be clean (no modifications from the HEAD commit)
Example:
git cherry-pick commithash
git cherry-pick master~4 master~2
Apply the changes introduced by the fifth and third last commits pointed to by master and create 2 new commits with these changes.
git stash#
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”
What does the command do?
Save you current status and revert the directory to
HEAD
Example:
git stash
: save your uncommitted changesgit checkout some_branch
: change to intended branchgit stash list
: lists all you stashesgit 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 master but it should not have been there
git reset HEAD~ --hard
: removes the last commit from the master branch
You did something wrong , accidentally deleted or just to remove stuff you tried that broke the repo
git reflog
: you will see all changes you did, even across branchesFind the one before you broke everything
git reset HEAD@{index}