Skip to content

Latest commit

 

History

History
210 lines (147 loc) · 5.92 KB

git.md

File metadata and controls

210 lines (147 loc) · 5.92 KB

Git - misc notes/tips

Branches

Rename branches

  1. git branch --move old_branch new_branch
  2. git checkout new_branch
  3. git push --set-upstream origin new_branch
  4. git push origin --delete old_branch

Tags

Show tags

git tag -l

Show tag details

git show TAG_NAME

Create an annotated tag

  1. git log --oneline -n 5
    • assuming that five lines is enough to spot the commit you want to tag, which is usually a merge commit
  2. git tag -a v0.1.5 -m "Tagging v0.1.5" GIT_COMMIT_HERE
  3. Push the tag(s)
    • All tags: git push origin --tags
    • Just the one: git push origin TAG_NAME

Delete tags

Remote

  • git push origin :TAG_NAME
  • git push -d TAG_NAME
  • git push --delete origin TAG_NAME

Local

git tag --delete TAG_NAME

Display changes

Diff for specific commit

  • git diff COMMIT~ COMMIT
  • git show COMMIT
    • git log -p c -1 seems to do the same thing
  • git diff COMMIT^!
  • git diff-tree -p COMMIT

Diff for last commit

  • git show HEAD~1

Files modified

  • git show --name-only COMMIT
  • git diff-tree --no-commit-id --name-only -r COMMIT

Log display options

  • git log --all --decorate --oneline --graph

    • Short commit hash
    • One line log summary
    • graph showing branch merges/activity
  • git log --graph --abbrev-commit --decorate --date=relative --all

    • lists a graph alongside the normal/detailed log output
    • TODO: List what the other options do
  • git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

    • TODO: Add description

Display configured remote

  • git branch -vv
    • shows info for all branches

Example output:

$ git branch -vv

* initial-dev-env-work e34d574 [origin/initial-dev-env-work] Initial version of mysql2sqlite-dev project
  master               14e28a5 [origin/master] Initial commit
  • cat .git/config
    • tip: look for [branches]

Change configured remote

one branch

  1. git checkout BRANCH_NAME
  2. git branch --set-upstream-to REMOTE/BRANCH_NAME

all branches

Assuming that pushing changes to the remote is OK, this seems to do the trick:

  1. git push origin -u --all

Example output:

Everything up-to-date
Branch 'initial-dev-env-work' set up to track remote branch 'initial-dev-env-work' from 'origin'.
Branch 'master' set up to track remote branch 'master' from 'origin'.

Reset date

Reset date of last commit

  1. git commit --amend --date=now

Reset date of specific commit

placeholder

Reset author/email

Reset author of last commit

  1. git commit --amend --reset-author --no-edit

This resets commit author values using the information specified by git config user.name and git config user.email.

Reset author on ALL commits

git filter-branch -f --env-filter "
    GIT_AUTHOR_NAME='Newname'
    GIT_AUTHOR_EMAIL='new@email'
    GIT_COMMITTER_NAME='Newname'
    GIT_COMMITTER_EMAIL='new@email'
  " HEAD

Configure cached credentials via .netrc (e.g., for automated builds)

This approach is useful if you want to setup a build system that requires read-only access to a restricted repo. Instead of hard-coding your main set of credentials (which are likely protected by 2FA), you can often request an API key via the web UI for select repo operations.

  1. Create $HOME/.netrc
  2. Enter machine, login and password details
  3. Test!

Example contents:

machine example.visualstudio.com
login bob
password afg87afg8a7fga8sfg78afga8fgafs8dgsdfagxgcxcfgfrg87as

git clone operations should work as expected without prompting for credentials.

References