How to edit an incorrect commit message in Git
If the commit is the last one:
You can also use rebase to edit the commit messages – choose ‘reword’ for the commits you wish to edit.
Warning: Doing a rebase does change the commit history – it changes the SHA of commits – so do not rebase a commit that has already been published otherwise you will end up with duplicated commits when your team merges your branch with their own branch.
Protip: You can ignore that rule if you are working on a feature branch and you are about to merge your work with
master and you just want to clean up things. Since you are going to be merging to master and then deleting the feature branch nobody is going to be affected. It is actually a good practice to do that in such a case.
Trivia: You can’t rebase the first commit of a repository, unless you pass the
How to undo the last Git commit
How to delete a git branch both locally and remotely:
What is the difference between ‘git pull’ and ‘git fetch’
git fetch: Updates your local repository with the data from remote.
git pull: Updates your working copy with the changes in the remote.
In practice: If you want to get the changes from remote without immediately changing your working copy then use
git fetch. Otherwise use
git pull does a
git fetch followed by a `git merge.
How to undo ‘git add’ before having commited
How to merge a Git conflict
A conflict in Git occurs when two branches happen to modify the same area of code. Say you have a branch A:
…while branch B have this code in the same line:
In such case Git is not capable of merging both changes because Git can’t possibly know which one to choose. Your manual intervention is necessary and that is what we call a Git conflict. Git merging tools will markup areas in your code with conflict markers.
After seeing a conflict, you can do two things:
Decide not to merge.
git merge --abortcan be used for this.
Resolve the conflicts. Git will mark the conflicts in the working tree. Edit the files into shape and
git addthem to the index. Use
git committo seal the deal.
Anatomy of a conflict marker:
Following the example, if we try to merge John’s branch in Jaime’s branch we would have had:
Understanding the intention behind each diff block is generally very helpful for understanding where a conflict came from and how to handle it.
This shows all of the commits that touched that file, considering just changes in between the common ancestor and the two heads you are merging, so it doesn’t include commits that already exist in both branches before merging. This helps you ignore diff blocks that clearly are not a factor in your current conflict.
After resolving the conflicts, it is a very good practice to test that you didn’t broke anything. Run your automated test suite.
The easiest conflicts to solve are the ones that never happened:
- Talk to your team and if you anticipate that some of you are going to be doing extensive modifications to a single file consider instead working sequentially – one of you make your changes first, and the other can work on the top of said changes. No conflicts.
- If working in parallel is a must, then merge assiduously; that way you will catch conflicts sooner than later – and so will be smaller and fresher in the memory of both parties involved.
If there is something worse than a difficult merge is a merge that went wrong but got commited anyways. If you find yourself unable to resolve the conflicts with confidence, do not merge, instead abort the merge with
git merge abort and talk to your team on how to best tackle it.
How to clone all remote branches with Git
How to remove all untracked files in Git
How to remove a git submodule
How to make an existing Git branch track a remote branch
How to do a “git export” (like “svn export”)
How to rename a local Git branch
How to add an empty directory to a git repository
Currently the design of the git index (staging area) only permits files to be listed. Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.
If you really need a directory to exist in checkouts you should create a file in it. For example a
.gitignore file; you can leave it empty.
How to checkout a remote branch
Checkouts a branch named my_branch that exists in any of the remotes.
How to revert to previous Git commit
There are two cases:
- If you have published it already:
- If you haven’t publish it already
How to force git to overwrite local files on pull
Update from remote.
Reset the master branch to what you just fetched. The
--hard option changes all the files in your working tree to match the files in origin/master, so if you have any local changes, they will be lost. With or without
--hard, any local commits that haven’t been pushed will be lost.
How to stash only one file out of multiple files that have changed
This will stash everything that you haven’t previously added. Just git add the things you want to keep, then run it.