Skip to content

Instantly share code, notes, and snippets.

@smolin
Last active January 12, 2021 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smolin/98ad742e604734415a130bb20231bf49 to your computer and use it in GitHub Desktop.
Save smolin/98ad742e604734415a130bb20231bf49 to your computer and use it in GitHub Desktop.
notes on git and github
# Github
## fatal: The upstream branch of your current branch does not match the name of your current branch.
git branch -u origin/$(git rev-parse --abbrev-ref HEAD)
# Undo
## undo a commit locally, not pushed to github
git reset HEAD~
# cf https://stackoverflow.com/a/927386 "leaves ... the state of your files on disk ... unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status, so you'll need to add them again before committing)."
## undo a commit accidentally pushed to github
git checkout my-pull-request-branch
git rebase -i HEAD~n // where n is the number of last commits you want to include in interactive rebase.
Replace pick with drop for commits you want to discard.
Save and exit.
git push --force
# cf https://stackoverflow.com/a/51400593
# merging and squashing
## squash series of commits down to one with relative id:
git reset --soft HEAD~3 # "git reset --soft HEAD" is a no-op
git commit
cf https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git/5201642#5201642
# Find (a suitable) recent common ancestor of two branches:
git merge-base branch-a branch-b
# Rebase: first find common ancestor, then squash to one commit LATER, then rebase
# You see "Your branch and 'origin/master' have diverged, and have 13 and 2 different
# commits each, respectively." but you want to bring local repo in alignment with remote.
# ref: https://stackoverflow.com/a/28441119
git reset --hard @{upstream}
git pull
# ssh and github
only use git@github.com: paths, not https:// paths
on local machine run:
git config --global user.name "Steve Molin"
git config --global user.email stevemolin@gmail.com
ssh-keygen
ssh-agent bash
ssh-add ~/.ssh/id_rsa
on github.com go to settings > ssh keys > add, and paste public key
test with ssh -T git@github.com
# create a new branch (locally)
git branch DEV-303
# focus on a branch in order to work on it (checkout)
git checkout DEV-303
# take unstaged, uncommited work from master to a new branch (does not clean up master, though):
git checkout newbranchname
# push a new local branch to repo on Github where it doesn't exist yet:
git push -u origin newLocalBranch
# replay one or more commits from one branch onto another:
git checkout <target>
git cherry-pick 6a58184 56af42c 543b7bc
# cf http://think-like-a-git.net/sections/rebase-from-the-ground-up/cherry-picking-explained.html
# 'rebase' conceptually is this: in branch 'target', reverse to common ancestor of branch 'source';
# then apply commits from 'source'; then apply the commits from 'target' that you reversed in step 1.
# Here's a decent explanation: https://www.atlassian.com/git/tutorials/merging-vs-rebasing
#
git checkout source
git pull
git checkout target
git rebase source
#
# if after resolving a conflict and marking with 'git add' you get the message "No changes - did
# you forget to use 'git add'?" just do 'git rebase --skip'
# cf http://wholemeal.co.nz/blog/2010/06/11/no-changes-did-you-forget-to-use-git-add/
#
# Now you're resolved all conflicts, right? If you run 'git status' you might get the scary
# message 'Your branch and 'origin/container-foundry' have diverged ... use "git pull" to
# merge the remote branch into yours)'. Chances are this is not actionable, and you want to
# do 'git push' instead.
#
# if 'git push' give you the message 'Updates were rejected because the tip of your current
# branch is behind its remote counterpart' you need to do 'git push -f'
# cf http://serebrov.github.io/html/2012-02-13-git-branches-have-diverged.html
# start a new repo on github starting with some local files:
# cf https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
[create repo on github w/o README or other files]
git init ; git add <files> ; git commit
git remote add origin <github url>
git push -u origin master
# grab a branch from github instead of trunk:
git clone <something>
git checkout -b 2017 --track origin/2017
# undo a commit and redo
# cf https://stackoverflow.com/questions/927358/how-to-undo-the-most-recent-commits-in-git#927386
$ git commit -m "Something terribly misguided"
$ git reset HEAD~ # undo commit, leaving files as before commit (modified, unstaged)
<< edit files as necessary >>
$ git add <<some files>>
$ git commit -c ORIG_HEAD
# include some lines from another branch
# cf https://stackoverflow.com/questions/10784523/how-do-i-merge-changes-to-a-single-file-rather-than-merging-commits/11593308#11593308
git checkout some_branch
git checkout --patch some_other_branch some_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment