Skip to content

Instantly share code, notes, and snippets.

@Raffo
Last active April 29, 2019 08:14
Show Gist options
  • Save Raffo/4a7b94cb80485e0ad320 to your computer and use it in GitHub Desktop.
Save Raffo/4a7b94cb80485e0ad320 to your computer and use it in GitHub Desktop.
Saving some common GIT patterns

Keep a branch in sync with master

git checkout master
git pull
git checkout branchname
git merge master

I made some changes on the master but I want to bring (and commit) them to a different (NEW) branch.

git stash save
git checkout -b <new_branch>
git stash pop

Same as above but to an existing branch

git stash save
git checkout <branch_name>
git stash pop

Cloning only a subpart of a Github repository

git clone https://github.com/theleagueof/chunk
git subtree split --prefix=folder_name -b new_branch
git remote add upstream https://github.com/user/repo
git push upstream new_branch
# then create a master branch

Tag

git tag -a v0.2 -m "Version 0.2 of chimp"
git push origin v0.2

Delete a remote tag

git tag -d 12345
git push origin :refs/tags/12345

Move some commits on master to another branch

git branch newbranch
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.*1
git checkout newbranch

Configure upstream repository for a "fork repo"

# https://help.github.com/articles/configuring-a-remote-for-a-fork/
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

I messed up the commit history on my branch and I don't really care. I just want to have a fresh commit in the branch with all the changes I made.

Yeah it's bad, but still it happens. Of course you can do a rebase, but sometimes this is annoying or complicated. A (bit weird) alternative is the folllowing:

git checkout feature_branch
git reset --soft master # keeps the changes, gets rid of the commit history
git add -A # pay attention to that!
git commit -m "Committing all at once."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment