Skip to content

Instantly share code, notes, and snippets.

@saninmersion
Forked from puncoz/git-commands.md
Created July 5, 2018 04:20
Show Gist options
  • Save saninmersion/06cd12aaf34d80eb2f75d18233d65d06 to your computer and use it in GitHub Desktop.
Save saninmersion/06cd12aaf34d80eb2f75d18233d65d06 to your computer and use it in GitHub Desktop.
Useful git commands

Useful git commands

Get local branch count

git branch | wc -l

Delete all merged branch from local

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Update only date time of last commit

git commit --amend --date="$(date -R)" This will trigger a 'git commit --amend' action

Update author info on last commit

git commit --amend --author="Puncoz Nepal <info@puncoz.com>" This will trigger a git commit --amend action

View git configs

gcf This works on ZSH shell

To count the commits

for the branch you are on:

git rev-list --count HEAD

for a branch

git rev-list --count <branch-name>

If you want to count the commits on a branch that are made since you created the branch

git rev-list --count HEAD ^<branch-name>

Squash

git rebase -i HEAD~5

Untrack files from git

git rm -r --cached <file>
git add -u

Rename branch

1. Rename your local branch.

If you are on the branch you want to rename: git branch -m new-name If you are on a different branch: git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch.

git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch.

Switch to the branch and then: git push origin -u new-name

Summarize git log output

git shortlog -s -n

Migrate repository from one repo to another

// Fetching all branches and tags from old repo
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git fetch --all`
git pull --all

// Pushing into new repo with remote name new-repo
git push -u new-repo --all
git push -u new-repo --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment