Skip to main content

Git

Nice aliases

alias gl='git pull'
alias gco='git checkout'
alias gcb='git checkout -b'
alias ga='git add'
alias gaa='git add -A'
alias gr='git reset'
alias gcm='git commit -m'
alias gcma='git commit --all -m'
alias gca='git commit --amend --no-edit'
alias gcam='git commit --amend -m '
alias gcaa='git commit --all --amend --no-edit'
alias gcaam='git commit --all --amend --no-edit -m'
alias gp='git push'
alias gpf='git push --force-with-lease --force-if-includes'
alias gst='git status'
alias gd='git diff'
alias gm='git merge'
alias grb='git rebase'
alias gsh='git stash' # not great, but conflicts with git status
alias glg='git log'
alias glo='git log --oneline'

# remove all local branches which have been merged, excluding master/main/dev
alias git-delete-merged='git branch --merged | egrep -v "(^\*|master|main|dev)" | xargs git branch -d'

Project

"forking" to local

https://www.baeldung.com/ops/git-clone-remote-branches

git clone --mirror https://github.com/exampleuser/exampleproject.git
cd exampleproject.git
git config --bool core.bare false
    (I had to do this before the next step:)
    cd ..
    mk exampleproject
    mv exampleproject.git exampleproject
    cd exampleproject
    mv exampleproject.git .git
git reset --hard

hard kill/delete/remove of all not tracked/untracked files, reset project

#show what will be deleted
git clean -dx -n
#DELETE, CAREFUL
#reminder to backup application.yml, run configurations, notes, etc.
git clean -dx -f

auto setup branch on origin when pushing

git config push.autoSetupRemote true

Local branch management

update master from any branch

git push . origin/master:master

reset local branch to origin state

git fetch origin
git reset --hard origin/master

rename LOCAL branch

git branch -m <newname>
# to push to origin, push new branch and delete old one
git push origin -u <newname>
git push origin --delete <oldname>

stash

stash only staged files

git stash push -S

stashing specific files

#only works for tracked files. Workaround -> add file, then stash
git stash -- file1.txt file2.txt
#I think always works?
git stash push file1.txt file2.txt

staging

only add parts of a change

git add -A --patch
git add --patch <filename>
git add --patch <filename>|'pattern'

changing history

remove not-the-last commits

git reset -i HEAD~10
# where 10 is the number commits to look back

changing commits

https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

remove/change previous commit

git reset --soft head~

To split a previous commit into multiple commits:

git rebase -i
#in the editor, at the commit you want to split up, change pick to edit
git reset HEAD^
#now add changes and commit as desired

To add changes to a last commit, super easy:

git add <change>
git commit --amend

And to change the commit message

git commit --amend -m "new message"

hard reset one file

git checkout HEAD <pathtofile>

diff

# see staged changes
git diff --cached
# see changes in a commit:
git diff COMMIT~ COMMIT
# or maybe
git show COMMIT

history

# find file that was deleted at some point in the git history
git checkout master (or latest)
# concise form, to see full history of file remove the "-1"
git log --full-history -1 -- <file>

Misc

wildcard use

git add/reset/checkout '*search*'
git add folder
git add folder/*

find remote url

git remote -v