Skip to content

Instantly share code, notes, and snippets.

@mencargo
Last active March 19, 2024 21:43
Show Gist options
  • Save mencargo/4a83e29d4b2eabd9e0acd0b489118157 to your computer and use it in GitHub Desktop.
Save mencargo/4a83e29d4b2eabd9e0acd0b489118157 to your computer and use it in GitHub Desktop.
GIT stuff

BASH (~/.bashrc)

# Simple git prompt
COLOR_GIT_CLEAN='\[\033[1;30m\]'
COLOR_GIT_MODIFIED='\[\033[0;33m\]'
COLOR_GIT_STAGED='\[\033[0;36m\]'
COLOR_RESET='\[\033[0m\]'

function git_prompt() {
  if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
    branch_name=$(git symbolic-ref -q HEAD)
    branch_name=${branch_name##refs/heads/}
    branch_name=${branch_name:-HEAD}
    echo -n " → "
    if [[ $(git status 2> /dev/null | tail -n1) = *"nothing to commit"* ]]; then
      echo -n "$COLOR_GIT_CLEAN$branch_name$COLOR_RESET"
    elif [[ $(git status 2> /dev/null | head -n5) = *"Changes to be committed"* ]]; then
      echo -n "$COLOR_GIT_STAGED$branch_name$COLOR_RESET"
    else
      echo -n "$COLOR_GIT_MODIFIED$branch_name*$COLOR_RESET"
    fi
  fi
}

function prompt() {
  PS1="\u@\h [\w$(git_prompt)] \$ "
}

PROMPT_COMMAND=prompt

SSH (~/.ssh)

Generate keys with ssh-keygen, the public key should be at ~/.ssh/id_rsa.pub

GIT (~/src/repo/.git)

Upload an existing project to a remote repository, go inside your project's root folder and:

git init
git remote add origin <url>
git add <files>
git commit -m "<message>"
git push --set-upstream origin master

Start a new project from a remote repository just git clone <url> <new_folder>

Go into the <new_folder>, do your thing, then git add <files>, git commit -m "<message>" and git push

Git stuff:

~/src/repo/.gitignore

.*
*.out
*.o
*.a

Depending on the project you could add to the list: main.c, *.sh, *.pdf...

Check the Git Cheat Sheet from GitHub.

Add stuff:

git add -f .gitignore to force adding a file even if it's on the ignore rules

git add --all add all files from all paths within your repository (follows ignore rules)

git add . add all files from the current folder (also follows ignore rules)

git commit -a -m "<message>" will add and commit ANY modified files that have already been added to the repository

Remove stuff:

git rm -rf <wrong_file/folder> careful, it's a single command to remove, stage, commit and push

Completely revert lastest commit:

git reset --hard HEAD^
git push origin -f

git checkout master -f <file> revert changes to match the current commit, could be the entire repo without <file>

git remote set-url origin <url> changes an existing remote repository <url>

Completely messed up a local repo and want to start over?

Could try rm -rf .git in the root repository folder

Check stuff:

git status pay attention to the messages

git log check that your HEAD is in the same commit as origin/master

git ls-files list of staged files (not necessarily pushed!)

git diff <file> see the current changes, checks all files if you don't specify some

git diff-tree --no-commit-id --name-only -r <commit-hash> list of files staged at a specific commit

git checkout <commit hash> go to a specific commit (get the hash from the git log)

git checkout master go to the latest commit

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