In this post, we will learn to do the following
- Delete a branch remotely.
- Merge specific file from one branch into another branch.
- Create shortcuts for git commands.
- Diff between branches.
- Undo local commit.
- Undo remote commit.
- Stash command.
- What is cherry-pick?
1. Delete a branch remotely
Have seen many projects with lot of orphan branches. Git encourages branching but sometimes you need to do a clean up and delete merged or orphaned branches from your local and remote repositories.
git push origin --delete branchname
2. Merge specific file from one branch into another branch
Many times i ended up having to just merge specific files from another branch into current branch. For example we want to merge the index.php file from a feature branch or master branch to our development branch. Here is how we can do that.
First checkout the target branch, the branch where we need to merge the file into. In our case it is development branch
git checkout development
Then lets merge the index.php from master into development using
git checkout --patch master index.php
This will create a patch from the index.php from the master branch and merge it into current branch.
3. Create shortcuts for git commands
You can create shortcuts for your favorite git commands using alias. It is something like the bash alias, but git aliases can only be execited using git command. like
While development you will have to check the difference between master and development branches, right? So lets create an alias with name
whatsnew to execute
git diff master development
git config alias.<alias_name> "<git_command>"
git config alias.whatsnew "diff master development"
You can add
config in the above example to make this git alias accessible from all repo on your machine.
From now on, when ever you need to check the repo difference just execute
git whatsnew from command line.
4. Diff between branches
git diff can be used to see the diffences between two branches, commits or even files.
Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes between two blob objects, or changes between two files on disk. -- from official Git doc
Let us check 3 different usage of git diff
Shows the difference between last commit and the not staged files.
Files which are staged for commit will not be shown.
git diff --staged
Shows the diffferences between the files already staged for commit and the latest commited version.
git diff <first branch> <second branch>
Shows the differences between two branches.
git diff <commit id> <commit id>
Shows the differences between two commit ids given.
5. Undo local commit
Many times you may have commited your changes incorrectly and wanted to undo the commit, right? You are not the only one who have made this mistake. All developers make this mistake. With git you can undo the commit. Lets take a look on how we can do that.
git reset will uncommit last commit and unstage changes in the working tree.
git reset --soft HEAD~1 The
--soft flag peserve the changes made on the working tree, only commit is reverted.
git reset --hard HEAD~1 The
--hard flag uncommits and also removes the changes from the working tree. Use this only when you are sure the changes are not needed.
--soft: uncommit changes, changes are left staged (index).
--mixed (default): uncommit + unstage changes, changes are left in working tree.
--hard: uncommit + unstage + delete changes, nothing left.
6. Undo remote commit
TO remove a commit you already pushed to remote repository you have to delete it locally and then push your changes to remote.
git reset --hard HEAD~1
git reset --hard <commit_id>
Then you can push to the repo using
git push --force origin master
7. Git stash
git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
You can list them with
git stash list
You can stash changes from one branch and apply them to another branch. You can use
git stash apply to apply the latest changes stored in stack. This will not remove it from stack.
If you want to apply the changes from stack and also removes it from stack immediately use
git stash pop
You can remove it from stack using
git stash drop without applying it.
8. What is cherry pick?
Cherry pick command is useful when you want to pick only some changes instead of merging the whole feature branch to master or development (or any branch).
For that checkout the branch you want to apply changes to then run
git cherry-pick <commit_id>
You can do cherrypick multiple commit by
git cherry-pick <commit_id> <second_-commit_id>
When doing cherry pick conflicts may occur. After resolving the conflict use
git cherry-pick --continue