Skip to content

Instantly share code, notes, and snippets.

@truetone
Last active March 22, 2018 17:07
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 truetone/6419f9d0eeac5950f2bb to your computer and use it in GitHub Desktop.
Save truetone/6419f9d0eeac5950f2bb to your computer and use it in GitHub Desktop.
Semi-frequent git commands that I have to look up every time

Git Tips

Push New Remote Branch

git push -u origin feature_branch_name

Checkout Remote Branch

git fetch
git checkout test

If that fails try:

git checkout -b test origin/test

Delete Remote Branch

This will only work if you have a local copy of the remote branch.

git push origin :feature-branch # Note the colon before the feature branch name.

Submodules

Recursively Checkout All Submodules to master

git submodule foreach --recursive git checkout master

Recursively Pull All Submodules

git submodule foreach git pull

Remove a Submodule

git submodule deinit submodule-name
git rm submodule-name
git rm --cached submodule-name
rm -rf .git/modules/submodule-name # Linux
rm -Rf .git/modules/submodule-name # OS X

Reset

Reset to Specific SHA Hash

git reset <hash>

This will preserve current state of files, but git will consider them modified.

Hard Reset to Specific SHA Hash

git reset --hard <hash>

Use with caution. You will lose all local modifications.

Hard Reset to State On a Specific Date and Time

git reset --hard master@{"2014-08-26 13:15"}

Use with caution. You will lose all local modifications.

Reset Local Branch and Discard All Unstaged Changes

git checkout -- .

Force local repo to exact state of remote

git fetch origin
git reset --hard origin/master

Force remote repo to exact state of local

git push --force origin [branch]

Use with EXTREME caution.

Tags

List Tags

git tag -l

Checkout A Specific Tag

git checkout tags/<tag_name>

Create An Annotated Tag

git tag -a v1.4 -m 'my version 1.4'

Create An Annotated Tag That Points to A Specific SHA Hash

git tag -a v1.2 -m 'version 1.2' <hash>

Push Tags

git push --tags

Push A Single Tag

git push origin <tag_name>

Delete A Tag

git tag -d release01 # Deletes it locally
git push origin :refs/tags/release01 # Deletes it remotely

Move A Tag To Another Commit

Delete the tag on any remote before you push

git push origin :refs/tags/<tagname>

Replace the tag to reference the most recent commit

git tag -fa <tagname>

Push the tag to the remote origin

git push origin master --tags

List Files in Conflict

Useful if a lot of files change, but only a few are in conflict. Sometimes the files in conflict are hard to find with a simple git log command.

git diff --name-only --diff-filter=U

Recover a Deleted File from History

If you know the file name and path:

file="path/to/file/file_name.py"
git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

Delete All Branches Already Merged Into master

git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d

Delete All Untracked Files

Do Dry Run First

git clean -f -n

Delete For Real (Use w/ Caution)

git clean -f

Delete and Recursively Delete Directories

git clean -f -d

Delete Ignored Files

git clean -f -X

Delete Ignored and Non-ignored Files

git clean -f -x

Restore a Deleted File

Find The File

git rev-list -n 1 HEAD -- path/to/filename

Then Check the File Out

git checkout deletingcommitid^ -- path/to/filename

Add All Files Except One

git add .
git reset -- path/to/file/that/will/not/get/added/file.txt

Amend/Edit/Fix The Last Commit Message

git commit --amend  # or
git commit --amend -m "New commit message."

See Commits Only Made In The Current Branch

Assuming the branch was created off master:

git log master..

Stash

Apply A Specific Stash

git stash apply stash@{n}

Name a stash

git stash save "guacamole sauce WIP"

Retrieve named stash

git stash apply stash^{/guacamo}

Stash untracked files

git stash -u

Clear all stashes

git stash clear

Diffs

Run A diff On A File From Another Branch

git diff mybranch master -- path/to/myfile.py

See Files Changed in Branches

Run this in the branch you want to compare:

git diff --name-only <branch-to-compare> // List of the changed files

For example, to compare the current branch to develop:

git diff --name-only develop

Or to choose two branches to compare:

git diff --name-only <branch-1> <branch-2>

Upstream Forks

Set another remote as upstream

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

Pull upstream

git pull upstream branch_name
git pull upstream master

Subtrees

Add a subtree to a project

git subtree add --prefix path/to/directory git@github.com:user/git-project.git master --squash

Update a subtree

git subtree pull -P path/to/directory --squash git@github.com:user/git-project.git master

Create a new repo from a directory in an existing project

  1. Create a new repo
  2. add the new repo as a remote: git remote add new_origin git@github.com:username/project.git
  3. push to the new repo: git subtree push --prefix=path/to/directory new_origin master

Git will send the contents of the folder to the new repo along with and just the relevant commits.

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