Created Sun Jun, 09 2019 at 11:23AM

git revert

git push origin :bugfix/branch-baz (note the colon)

git reset --hard ABCDE

git push origin bugfix/branch-baz

config globals

git config --global user.name "Jaime Carranza"

git config --global user.email "Jaime@Carranza.co"

git config --global color.ui true

git config --global core.editor "vim"

git remote url

git config --get remote.origin.url

git delete remote branch

git push origin --delete the_remote_branch

oh my zsh remote sshfs slow

 # will hide it globally.
git config --global oh-my-zsh.hide-status 1

 # hide per repo
git config oh-my-zsh.hide-status 1

basics

git init # initializes a git project

git add . git add /path/foo.bar

git commit -am "this is a commit message"

git ignore

echo "logs\n*.log\ntmp" > .gitignore git add .gitignore git commit .gitignore -m "adding git ignore"

git patch

f you haven't yet commited the changes, then:

git diff > mypatch.patch
But sometimes it happens that part of the stuff you're doing are new files that are untracked and won't be in your git diff output. So, one way to do a patch is to stage everything for a new commit (but don't do the commit), and then:

git diff --cached > mypatch.patch
Add the 'binary' option if you want to add binary files to the patch (e.g. mp3 files):

git diff --cached --binary > mypatch.patch
You can later apply the patch:

git apply mypatch.patch

git log

git log --oneline

git log --oneline --graph

git log --pretty="%h, %cn, %cr" # * list provided in tail of file

gitk (tk gui)

git aliases

git has it's own way of creating aliases to internal methods example: we want to make shortcut "s" map to command status

Git Aliases

git config --global alias.s status git config --global alias.co checkout git config --global alias.cm commit

Bash Aliases
alias gs="git status"
alias gc="git commit"
alias gi="git add -i" # interactive add
alias gl="git log --oneline --graph --all --decorate"

git stashing

git stash takes the dirty state of your working directory

un-stashing / applying a stash

git stash list
stash@{0}: WIP on feature/GS-1761-a....
stash@{1}: WIP on GS-1660-tsc....
git stash apply stash@{1}

viewing a stash

git stash show -p stash@{0}

dropping / deleting a stash

git stash drop stash@{1}

git revert file

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

git patch

git add -p goes hunk by hunk in a file. (s - split hunk)

git remote

git remote add example: git remote add origin https://carranza.co/git/some/git/url

git push

pushing changes from our local git copy to a remote

git push origin master

git pull

git pulldown changes from remote to local git

git fetch

git fetch to pull down changes but allow us to merge them in ourselves

git merge origin/master master to go ahead and merge all the changes from the remote and our local master

git clone

git clone git://someurl

git pretty log format values

'%H': commit hash

'%h': abbreviated commit hash

'%T': tree hash

'%t': abbreviated tree hash

'%P': parent hashes

'%p': abbreviated parent hashes

'%an': author name

'%aN': author name (respecting .mailmap, see git-shortlog[1] or git-blame[1])

'%ae': author email

'%aE': author email (respecting .mailmap, see git-shortlog[1] or git-blame[1])

'%ad': author date (format respects --date= option)

'%aD': author date, RFC2822 style

'%ar': author date, relative

'%at': author date, UNIX timestamp

'%ai': author date, ISO 8601-like format

'%aI': author date, strict ISO 8601 format

'%cn': committer name

'%cN': committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1])

'%ce': committer email

'%cE': committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1])

'%cd': committer date (format respects --date= option)

'%cD': committer date, RFC2822 style

'%cr': committer date, relative

'%ct': committer date, UNIX timestamp

'%ci': committer date, ISO 8601-like format

'%cI': committer date, strict ISO 8601 format

'%d': ref names, like the --decorate option of git-log[1]

'%D': ref names without the " (", ")" wrapping.

'%e': encoding

'%s': subject

'%f': sanitized subject line, suitable for a filename

'%b': body

'%B': raw body (unwrapped subject and body)

'%N': commit notes

'%GG': raw verification message from GPG for a signed commit

'%G?': show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature

'%GS': show the name of the signer for a signed commit

'%GK': show the key used to sign a signed commit

'%gD': reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log -g refs/heads/master would yield refs/heads/master@{0}).

'%gd': shortened reflog selector; same as %gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).

'%gn': reflog identity name

'%gN': reflog identity name (respecting .mailmap, see git-shortlog[1] or git-blame[1])

'%ge': reflog identity email

'%gE': reflog identity email (respecting .mailmap, see git-shortlog[1] or git-blame[1])

'%gs': reflog subject

'%Cred': switch color to red

'%Cgreen': switch color to green

'%Cblue': switch color to blue

'%Creset': reset color

rename local and remote 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
  1. Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
  1. Reset the upstream branch for the new-name local branch.

Switch to the branch and then:

git push origin -u new-name

or

Renaming Git Branch

Follow the steps below to rename a Local and Remote Git Branch:

  1. Start by switching to the local branch which you want to rename:

git checkout <old_name>

  1. Rename the local branch by typing:

git branch -m <new_name>

At this point, you have renamed the local branch.

If you’ve already pushed the <old_name> branch to the remote repository , perform the next steps to rename the remote branch.

  1. Push the <new_name> local branch and reset the upstream branch:

git push origin -u <new_name>

  1. Delete the <old_name> remote branch:

git push origin --delete <old_name>

That’s it. You have successfully renamed the local and remote Git branch.

git delete tags

# oops you committed a tag but ended up pushing some extra files and now need to move the tag forward.
# remove from remote
git push --delete origin tagname
# remove from local
git tag --delete tagname
# now add tag like normal.

find git tags

 # list tags
git -l 
# list with filter
git -l 'multi*'

merge files from one branch to another branch

Found this excellent article explaining out to get out of a corner I painted myself into. It was brilliantly simple worked like a charm.

Use GIT Checkout

The gist being, simple switch into the branch you want to merge files into, then git checkout the files from the other branch.

git checkout my-target-branch
git checkout source_branch file2 path/to/file2 etc..

First, checkout to your BranchFoo:

git checkout BranchFoo

Then merge the BranchBar:

git merge BranchBar