Git has a wonderful tool that allows you to create patches of the commits in the tree of your project: git format-patch.
$ git format-patch [<options>] [<since> | <revision-range>]
More information can be seen via man page:
$ git format-patch -h
$ man git format-patch
$ git format-patch -N
This command generates one .patch file for each of the N commits on the top of your tree. Example:
$ git format-patch -1
Generates a .patch file for the commit on the top of your tree. Increase the number to get one file per commit (for the top 3 commits, use ‘-3’).
To apply the patch, use
$ git am [your-patch].patch
Egon Willighagen shares in his GitHub Tip: download commits as patches how to save a commit from github into a .patch file:
“By appending .patch to the commit URL, a commit can easily be downloaded as patch.”
Example using commit # 005cdb
Quick shot: To remove a remote branch on your git repo:
git push [remote-name] --delete [branch-name]
dkreling@dkreling:~/RobotFramework|devel ✔ ⟫ git branch -r
dkreling@dkreling:~/RobotFramework|devel ✔ ⟫ git push origin --delete teste [8:46:33]
remote: Updating references: 100% (1/1)
- [deleted] test
I recently started to work with two remote repositories on git, and then faced a probably common scenario: check the state of each repository (say ‘origin‘ and ‘vertiv‘).
A 20 minute search provided me the best solution:
$ git branch -u [remote-repo]/[branch]
In this case, origin/master is ahead of vertiv/master by 2 commits, and my local is sync’ed with vertiv/master.
The docs read:
-u <upstream>, –set-upstream-to=<upstream>
Set up <branchname>’s tracking information so <upstream> is considered <branchname>’s upstream branch. If no <branchname> is
specified, then it defaults to the current branch.
Find branches the commit is on
git branch --contains <commit #>
fetch_weather, implement_axios and master all have the commit # 3dea5b6
Show all branches with unmerged commits to <branch>
git branch -a --no-merged <branch>
remotes/origin/error_handler has not yet been merged into master
Happy git work!
I was in need of finding the commit that added a specific file in my project and “I got lucky” on Google:
Say we need the commit that introduced foo.js in the project, the command would be:
$ git log --diff-filter=A -- foo.js
And a super nice alias would be:
$ git config --global alias.whatadded 'log --diff-filter=A --'
and to use it:
$ git whatadded filename