Skip to content

Instantly share code, notes, and snippets.

@mohanraj-r
Last active February 27, 2019 03:09
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 mohanraj-r/d6d7a0884af78b497b21 to your computer and use it in GitHub Desktop.
Save mohanraj-r/d6d7a0884af78b497b21 to your computer and use it in GitHub Desktop.
[git] Git commands #shell #reference
# reset changes to a file
# http://stackoverflow.com/a/1817774
#
## Clone
## Clone only a single (feature) branch (from large repos)
git clone <repo> -b <branch> --single-branch --depth=1
# checkout a Pull request locally - https://help.github.com/articles/checking-out-pull-requests-locally/
# Fetch the reference to the pull request based on its ID number, creating a new branch
git fetch origin pull/ID/head:BRANCHNAME
# Commit sign existing commit
git commit -S --amend
# Abort a merge (conflict), as of version 1.6.1
# https://stackoverflow.com/a/102309
git reset --merge
# cleanout untracked files
git clean --force --interactive
# Assuming you did not commit the file, or add it to the index, then:
git checkout filename
# Assuming you added it to the index, but did not commit it, then:
git reset HEAD filename
git checkout filename
# Assuming you did commit it, then:
git checkout origin/master filename
# Assuming you want to blow away all commits from your branch (VERY DESTRUCTIVE):
git reset --hard origin/master
# reset local repo to point to remote state
# All local commits not common to the remote will be gone.
# http://stackoverflow.com/a/6284819
git fetch origin
git reset --hard origin/master
# list authors of a project
# http://www.commandlinefu.com/commands/view/4519/list-all-authors-of-a-particular-git-project
git log --format='%aN' | sort -u
# ordered by commits
git shortlog -s -n | cut -c8-
# gives you the tracking info for the branches
# http://stackoverflow.com/a/12538667
git branch -vv
# Cleaning local stale branches
git remote prune origin
# fetch and automatically prunes all stale references
git fetch -p
# update remote
git remote set-url origin <URL>
# Show updated remote
git remote -v
# change a branch to be the master using strategy=ours
# http://stackoverflow.com/a/2763118
# Bundling a repo into a single file (for emailing around or backup)
# http://stackoverflow.com/a/5578292
git bundle create /tmp/repo.git_bundle --all
# show diff of stash indexed at 0 (latest stash)
git stash show -p stash@{0}
# Checking out submodules
# https://stackoverflow.com/a/11358126
git clone <repo url> --recursive
# if you already cloned but forgot --recursive
git submodule update --init
# To apply changes in .gitmodules file
git submodule sync
git submodule update --init --recursive --remote
# Reverting changes to submodules
# https://stackoverflow.com/a/17871677
git submodule foreach --recursive git reset --hard
# Syncing up to changes in remote
git submodule foreach --recursive git pull
# Automatically add all submodules in a repo (when submodule is not browseable in github)
# ref: https://stackoverflow.com/a/10607225
for x in $(find . -type d) ; do
if [ -d "${x}/.git" ] ; then
cd "${x}"
origin="$(git config --get remote.origin.url)"
cd - 1>/dev/null
git submodule add -f "${origin}" "${x}"
fi
done
# Check if a hash is present in checked out repo
# https://stackoverflow.com/a/18516021
git cat-file -t <hash>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment