Skip to content

Instantly share code, notes, and snippets.

@mort3za
Last active April 22, 2019 06:46
Show Gist options
  • Save mort3za/4d17114915e00d314ca6b7f7fe412eb1 to your computer and use it in GitHub Desktop.
Save mort3za/4d17114915e00d314ca6b7f7fe412eb1 to your computer and use it in GitHub Desktop.
Some git commands and tips
# Add a ssh key to list of ssh keys of git
# first ensure that ~/.ssh/ folder is writable by current user. (if you are logged in as root can't solves this, change user by `su anotheruser` and then `chown anotheruser:anotheruser /home/anotheruser/.ssh/`)
# add a new key to .ssh/ folder of current user home.
ssh-keygen
# enter full address. `/home/anotheruser/.ssh/your_ssh_key_name`
# enter a passphrase
# if you used id_rsa as the name of key no need this step.
ssh-add `/home/anotheruser/.ssh/your_ssh_key_name
# enter passphrase
# done.
# now use this to clone a git repo
git clone git@github.com:...git
# Add a remote url for existing git repo
git remote add origin git@gitlab.com:x/y.git
# change existing git remote to another address
git remote set-url origin git@gitlab.com:x/z.git
git tag -l | xargs git tag -d
git fetch
# 1. Delete all tags from the local repo. FWIW, xargs places each tag output by "tag -l" onto the command line for "tag -d". Without this, git won't delete anything because it doesn't read stdin (silly git).
# 2. Fetch all active tags from the remote repo.
@mort3za
Copy link
Author

mort3za commented Jun 25, 2016

@mort3za
Copy link
Author

mort3za commented Apr 27, 2017

Search in commits:
git log --all --grep='my search term'

It's better to learn native grep if you are on Linux.

@mort3za
Copy link
Author

mort3za commented Apr 14, 2019

git tag -l | xargs git tag -d
git fetch

  1. Delete all tags from the local repo. FWIW, xargs places each tag output by "tag -l" onto the command line for "tag -d". Without this, git won't delete anything because it doesn't read stdin (silly git).
  2. Fetch all active tags from the remote repo.

@mort3za
Copy link
Author

mort3za commented Apr 14, 2019

git log -p (or --patch) -2 (show log and changes in one command. only last 2 commits.)

git log --name-status --relative-date (same name-only with modified/added, 2 weeks ago)

git status -s | --short (only file changes)

git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
(an=author name, who wrote changes directly. cn=commiter name, who applied commit to work)

git log -S 'function name to search in code'

git remote -v (show remote url)

git remote show origin (list remote branches, sync status of local to remote branches)

git ls-remote origin (list all remote refs including tags and hashes)

git branch -v (show last commit of every branch)

git branch -vv (show tracking branches + ahead/behind status => fetch to update)

git branch --no-merged master (show branches not merged to master)

git checkout --track origin/hotfix1 (shorthand for checkout -b hotfix1 origin/hotfix1)

git rebase --onto master feature1 feature1-patch (gets feature1-patch commits without commits of feature1, then rebases on master, in fact removes commits of feature1 from patch)

@mort3za
Copy link
Author

mort3za commented Apr 14, 2019

git diff master...feature (show diff of master and feature, but if master don't show commits of master which are not in feature) (another way of doing it: git diff $(git merge-base feature master) )

@mort3za
Copy link
Author

mort3za commented Apr 22, 2019

Create zip archive of project

git archive master --prefix='project/' | gzip > `git describe master`.tar.gz
git archive master --prefix='project/' --format=zip > `git describe master`.zip

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