-
Notifications
You must be signed in to change notification settings - Fork 36
user byrne git cheat sheet
The following git cheat sheet are the commands given to me by our very own git-god resident Jay Allen. He routinely gives me the best tips and tricks to help me better manage my repository and create better code commits.
This is a shell script that helps to create a new feature branch.
#!/usr/bin/env bash
if [[ -z "$*" ]]; then
echo "Usage: $(basename $0) NEW_BRANCHNAME"
exit
fi
REMOTE=$(git remote -v | egrep "openmelody/melody.git \(fetch\)" | awk '{print $1}')
git checkout -b "$*" $REMOTE/master
echo -n "Updating branch with latest Melody changes..."
git pull
Note from Jay: You can find this script in the Melody repo at tools/new-branch. Just cd into the Melody directory and type the command. There's some weird formatting above so copy/paste isn't a good idea.
To figure out what branches have been merged into core and are safe to delete I do this:
git branch -r --merged
Sample output:
prompt> git branch -r --merged
origin/bug-400-revision-histories
origin/bug-410-refactor-load-order
origin/bug-446-update-plugins-test
This works with branches that were merged smoothly. Often if a merge is borked, the commit messages need to be rewritten, etc. In which case, Jay will create a branch off of your branch, and then merge that one -- thus losing the trail to your branch. Lesson: follow the contribution instructions!
Note from Jay: That command tells you which branches are ancestors of your current branch. So basically, any branches shown are already contained in your current branch. If your current branch is up to date with openmelody/master, then you can find out what branches have been merged into Melody.
When I want to synchronize my master to be the same as the master I forked from, do this:
prompt> git branch --set-upstream master openmelody/master
Note from Jay: I'm still not convinced this is a good idea. If you're using tools/new-branch consistently, the state of your master branch should not matter because you're never in it.
Jay shared this great tidbit with me when I failed to provide adequate information in one of my commit notes:
# Checkout the branch you want to edit
git checkout bug-507-change-default-asset-folder
# Interactive rebase - Google it!
git rebase -i bd631e1e # This is commit you created this branch from
# when your editor pops up, look for the line with
# the commit note you want to edit and change the "pick" at the
# beginning of the line to "reword". Save and close, then edit the
# commit note in the new window that pops up
# to say what you're changing it to, save and close again
# Get the latest from GitHub and put _your_ changes on top of them
git remote update && git rebase openmelody/master
# Repush the branch forcibly to $repo
git push -f origin bug-507-change-default-asset-folder
Note from Jay: You should be careful with this one. Anytime you rebase (or do
git commit --amend
) you rewrite your history meaning that the commits you edit and all that follow them are replaced by new copies with different SHA1 IDs. Do it as much as you like on commits that have never been outside of your local repo but don't try to edit commits that you pushed a month ago and have been merged into Melody. You'll end up with a big mess.
That said, I use
gir rebase
,git rebase -i
andgit commit --amend
every single day and with almost every topic branch. It's a fantastic power tool and like any power tool you should read the manual before using. One last tip: with "git rebase -i" you can not only edit your commit notes but you can squash them together, delete one or more commits or even reorder them.