Skip to content

Instantly share code, notes, and snippets.

@nicolasdao
Last active July 2, 2023 07:59
Show Gist options
  • Save nicolasdao/cc186df894f2202368fabc5b38711ccb to your computer and use it in GitHub Desktop.
Save nicolasdao/cc186df894f2202368fabc5b38711ccb to your computer and use it in GitHub Desktop.
Git guide. Keywords: git git-lfs lfs

GIT GUIDE

Table of contents

Most common commands

Checking differences

Command Notes
git diff Shows the difference between your working tree(your current changes) and the HEAD of your repo
git diff --cached Shows the difference between the index tree(the changes that you've already staged) and the HEAD
git diff --color-words Format the differences so that it doesn't duplicate changed lines(before/after), and instead uses colors on the same line to highlight what has changed
git diff 2154af2 7d6a9b4 Show differences between 2 commits.
git diff branch_1..branch_2 --name-status Compare 2 branches and output the list of files that are different
git diff c45702c your-file Difference in a file from working directory and a specific commit.
git diff head Shows the difference between all your changes(total changes of the working and index trees) and the HEAD
git diff head~3..head your-file Difference in a file from 3 commits ago

Tagging

Command Notes
git tag List all existing tags for the current checked-out branch
git tag --delete tagname Delete tag locally
git tag -a v.1.2.2 -m "hello world" Add tag and a message on the head of the current checked-out branch
git tag -a v.1.2.2 -m "hello world" 12frt5 Add tag and a message on a specific commit
git tag -d your-tag-name Delete tag
git tag -l "v.1.2.*" Filter tags using regex for the current checked-our branch
git tag -n List tags WITH messages

Git Large File Storage aka git-lfs

GitHub can only manage files as big as 50MB. Over that limit, you must use git-lfs.

Install git-lfs

brew install git-lfs

To set it up for you user account:

git lfs install

To test it is installed:

which git-lfs

How to use git-lfs

git lfs track "*.psd" & \
git add .gitattributes

Install

Troubleshooting

Error in git-raw-commits: fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree

Execute this command:

git commit --allow-empty -n -m "Initial commit."

Annexes

.gitconfig

[alias]
        co = checkout
        st = status
        b = branch
        cp = cherry-pick
        a = add --all
        cm = commit -am
        pullo = pull origin
        pusho = push origin
        mt = mergetool -t kdiff3
        unstage = reset head
        rb = pull --rebase origin
        l = log --oneline
        pushm = push origin master
        pushs = push origin staging
        pusht = push origin test
        pushd = push origin dev
        pulls = pull origin staging
        pullm = pull origin master
        pullt = pull origin test
        pulld = pull origin dev
        cos = checkout staging
        com = checkout master
        cot = checkout test
        cod = checkout dev
        notpushed = "!f() { git log origin/$1..$1; }; f"
        keepMine = "!f() { git checkout --ours $1; git add $1; }; f"
        keepTheirs = "!f() { git checkout --theirs $1; git add $1; }; f"
        pr = "!f() { git checkout -b $1-master master; if [ ! -z "$3" ]; then git pull https://github.com/$1/$2.git $3; else git pull https://github.com/$1/$2.git master; fi }; f"
        doc = "!f() { \
                echo 'a         - git add --all [Add all current changed from the working dir to the staging tree]'; \
                echo 'b         - git branch [List all branches]'; \
                echo 'cm                - git commit -am [Commit all changes to the head]'; \
                echo 'co                - git checkout [Change to another branch]'; \
                echo 'com               - git checkout master [Change to the master branch]'; \
                echo 'cos               - git checkout staging [Change to the staging branch]'; \
                echo 'cot               - git checkout staging [Change to the test branch]'; \
                echo 'cp                - git cherry-pick [Cherry pick]'; \
                echo 'l         - git log --oneline [List latest logs]'; \
                echo 'keepMine <filePath>               - Resolve conflict by keeping my file'; \
                echo 'keepTheirs <filePath>             - Resolve conflict by keeping their file'; \
                echo 'mt                - git mergetool -t kdiff3 [Open the merge tool]'; \
                echo 'notpushed <commit hash> - Check if a commit has been pushed to the origin or not'; \
                echo 'pr <github requester username> <project name> [requester branch] - Get the pull request from requester. Default branch is master'; \
                echo 'pullm             - git pull origin master [Pull latest changes from origin master]'; \
                echo 'pullo             - git pull origin [Pull latest changes from origin for all installed branches]'; \
                echo 'pulls             - git pull origin staging [Pull latest changes from origin staging]'; \
                echo 'pullt             - git pull origin test [Pull latest changes from origin test]'; \
                echo 'pushm             - git push origin master [Push current head to origin master]'; \
                echo 'pusho             - git push origin [Push current the head of all your installed branches to origin]'; \
                echo 'pushs             - git push origin staging [Push current head to origin staging]'; \
                echo 'pusht             - git push origin test [Push current head to origin test]'; \
                echo 'st                - git status [Show the status of your current branch]'; \
                echo 'unstage           - git reset head [Move changes from head to the working dir. Usefull to stash]'; \
        }; f"
[color]
        ui = auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment