Links

Git

What is Git and how to use it.

What is Git?

  • Git is a popular open-source version control tool commonly used for code but can be used with any type of file
  • More information about Git can be found on its website

How do I Git it?

  • Git is typically installed by default on Linux distributions and MacOS however it will need to be installed on Windows
    • Windows: winget install git.git
    • Linux: sudo apt install git
    • MacOS: brew install git

Commands

General

# get help
git --help
man git
# download/clone repo
git clone <repositoryLink>
# view changes made since last commit or initial download
git status
# queue files for commit
git add <fileName>
git add *
# commit files
git commit -m "description of change"
# push files to branch
git push
git push -u origin <branchName>
git push --set-upstream origin <branchName>
# view git log
git log
git log -p <commitHash>
# checkout a commit
git checkout <commitHash>

Branches

# view all local branches
git branch
# view all remote branches
git branch -r
# view all local and remote branches
git branch -a
# create new branch and switch to it
git checkout -b <newBranchName>
# switch branch
git branch <branchName>
# rename branch
git branch -m <oldBranchName> <newBranchName>

Modify Terminal to show current git branch

  • Update your shell's configuration file e.g., ~/.zshrc or ~/.bashrc
  • Save and then reload your shell for the changes to take effect source ~/.zshrc
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
# color #s found here: https://misc.flogisoft.com/bash/tip_colors_and_formatting
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '