without cd-ing into it. You can specify it using the $GIT_DIR or --git-dir options: • GIT_DIR=~/myproject/.git \ git <command> You can also specify a dislocated work tree using $GIT_WORK_TREE or --work-tree.
results in a detached head state. This means that the current working copy isn‘t the HEAD revision of any named branch. To create a branch at that point, simply: • git checkout -b new-branch-name
time: • <refname>@{<date>} Find the first object preceding another object: • <refname>^{<type>} Find the first object that contains a message: • <refname>^{/<regex>} Find the youngest commit by message: • :/<regex>
working copy and stage the changes: • git add . Commit the changes using the --amend option: • git commit --amend Don‘t amend a commit you‘ve already pushed. Unless you know what you‘re doing.
helps you search for changes that introduced a bug. • git bisect start • git bisect bad • git bisect good d34db33f • ... git checks out out next revision, test... • git bisect (good|bad) • git bisect reset
scripts available, you can use bisect to automate this: • git bisect run script [arguments] If the script exits with a 0, the revision is marked as good, otherwise a code between 1 and 127 means bad. 125 means untestable. After finding the commit, run server/bin/execute [username] to notify the firing squad.
in history, use the revert command: • git revert (<range>|<commit>...) This creates a new commit that applies an inverse patch of all the listed commits. Sometimes it‘s preferable to take out the merge commit instead of re-reverting it.
hash, effectively leaving behind a dangling branch: • git reset <target> --hard You can then create a named branch: • git checkout -b <branch-name> This is the preferred way of reverting a merge. This modifies history, think before pushing!
history on the remote, use the force flag: • git push origin branch-name -f DO NOT DO THIS! This overwrites all history for the pushed branch, effectively obliterating anything on the remote your history does not already contain.
warning and force pushed modified history or deleted your own. Don‘t panic. Every git operation is logged in the reflog and can be navigated by simply checking out the logged history. Just don‘t run git gc.
operations applied to your repository in the past few days: • git reflog [log options] [--verbose] You can then use git reset to return to the state at the moment you decided that drinking a bottle of absinthe yourself merging an unfinished branch was a good idea.
from commits: • git format-patch [<since>|<range>] <since> selects all commits from that commit up to the current revision (even if detached). To apply a patch: • git am < 0001-commit-message
creating patches out of commits and applying them to the current branch as commits. • git cherry-pick (<range>|<commit>...) \ [--edit] [-x] [--no-commit] For each merge conflict, resolve and run: • git cherry-pick --continue
git checkout master • git cherry-pick ^master legacy -x This creates one commit per cherrypicked commit. If you want a single commit: • git cherry-pick ^master legacy -n • (review, edit and TEST!) • git commit
you want to overwrite a file‘s contents with a specific commit or branch, you can check out that file directly: • git checkout my-branch -- <path>... • git add . • git commit
and appends them to the target. It‘s like merging, but avoids merge commits in favour of a more linear history. Works exactly like cherry-pick, but can be used automatically when pulling changes from upstream.
to sync with upstream. No worries: • git rebase <commit> You can do the same if you‘re working on the same branch and pulling from remote: • git pull origin my-branch --rebase This will only rebase outgoing commits onto the appended upstream history.
branch that has another branch attached. Rebasing the base branch will rebase that branch but leave the other unchanged (duplicated history). You need to rebase that branch separately to avoid duplicating and mangling history for everyone.
rebase instead of merge. This avoids merge commits each time someone pushes to the same branch. • git config branch.autosetuprebase always • git config branch.<name>.rebase true • git pull origin my-branch Caution. May cause cancer.
pick 7f3019b Removed APASUT overrides from all SDKs pick f83aba2 Added APASUT flag to all SDKs pick 5c335bc Flag checking pick 8363137 typo # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell
fix the commit messages for the last 3 commits. $ git rebase -i HEAD~3 reword 01 Added you‘re keyring reword 02 Fcuk u Smasung! reword 03 fixed alot of bugz