Skip to content

Instantly share code, notes, and snippets.

@mattfelsen
Last active March 2, 2016 22:36
Show Gist options
  • Save mattfelsen/e174f032fa10fa16cfb5 to your computer and use it in GitHub Desktop.
Save mattfelsen/e174f032fa10fa16cfb5 to your computer and use it in GitHub Desktop.
Useful Git Commands

Commits

Undo last commit

git reset --soft HEAD~1

Add stuff to last commit

git add file.cpp
git commit --amend

Squash last few commits without rebase

n = number of commits to squash

git reset --soft HEAD~n
git commit -m "New message"

Committed stuff on master instead of a feature branch?

Move all your commits that haven't been pushed yet to a new branch! From master...

# create new branch with this history
git checkout -b feature-branch master
# go back to parent branch
git checkout master 
# reset this branch's history back to origin
git reset --hard origin/master

Diffing

Commits on a branch compared to parent

git log <parent-branch>..

Show files from a commit

git show --name-status <commit_sha>

Show history of a file

--follow flag is useful if filename has changed

git log --follow file.cpp

Show the actual changes to a file

git log -p file.cpp

Diff a file between branches

git diff master develop -- file.cpp

Tags

create a "lightweight" tag with

git tag 0.1

push to remote with

git push --tags

show which commits a tag is pointing to

git show-ref --tags


Branches

switch to an existing branch with

git checkout foo

create a new branch based on your current branch and switch to it with

git checkout -b foo

or create a new branch based on a different branch

i.e. if you're on feature-something but want to create a new branch based on develop

git checkout -b feature-new develop

delete a local branch

git branch -d branch-name

delete a local branch that hasn't been merged, or merged but not pushed to the remote

git branch -D branch-name

delete a remote branch

git push origin :branch-name

show which branches have been merged into the current branch

If you want to clean up a bunch of branches and want to know what has been merged so you know what you can safely delete

git branch --merged


Remotes & Tracking

list all remotes

git remote -v

show which remote & branch each local branch is tracking, and commit hash

git branch -vv

change which remote and/or branch a local branch is tracking

From the branch you want to modify..

git branch -u remote-name/branch-name

push while setting the tracking remote & branch name

You need to do this the first time you push a newly created branch

git push -u remote-name/branch-name

add a second remote for pushing

Useful if you maintain an addon on your account, and a fork somewhere else (e.g. you've forked your repo into your company's account) and want to keep both in sync without needing to push twice

git remote set-url --add --push <new-remote-url>


Submodules

add/register a submodule

git submodule add http://server/path.git
# or if you want to specify the folder and/or path/to/folder to clone into
git submodule add http://server/path.git folder-name

pull that submodule down (ie clone)

git submodule update --init

recursive cloning, if your submodules have their own submodules

git submodule update --init --recursive

update your submodule's settings (i.e. the remote they track) if you've hand-edited .gitmodules

git submodule sync
git submodule update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment