Git & GitHub Basics

Created: September 24, 2024 5:17 PM

Setup on a Local Machine

git init  # initiate a git project
git status  # check local environment
git add <filename>  # staging a file

git config --global user.email "chriswang2025@u.northwestern.edu"
git config --global user.name "Chris Wang"

git commit -m "Create the initial message"
# insertions mean lines
git log  # to see who and when committed (see log)

GitHub Setup

  • Create a new repository
  • Get the repository’s URL
git remote add origin https://github.com/BellowAverage/HelloWorld.git
git branch -M main  # rename the current branch (master) to main
git push -u origin main  # origin is the name of a repository

Commonly Used Commands

git branch  # see current branch
git branch -r  # list all branches available
git checkout develop  # switch to another branch named develop, for example
git checkout -b <branch_name>  # Creates and switches to a new branch
git merge <branch_name>  # Merges the specified branch into the current branch.
git push -u origin <branch_name>  # Pushes the current branch to the remote repository and sets the upstream branch.
git pull  # Fetches and merges changes from the remote repository into the current branch
# -------------------------
# 3 most commonly used git command recommended by professor Thomas Miller
git status  # to see where I'm at
git add .  # take care of all the files
git commit -m "<message>"

Concluded by ChatGPT from Atlassian Knowledge Base

CommandDescriptionExample
git initInitializes a new Git repositorygit init
git clone <repository_url>Clones a repository to your local machinegit clone https://github.com/user/repo.git
git config --global user.nameSets your Git username globallygit config --global user.name "Your Name"
git config --global user.emailSets your Git email globallygit config --global user.email "your.email@example.com"
git statusDisplays the status of your working directorygit status
git add <file>Adds a file to the staging areagit add myfile.txt
git add .Adds all changes in the current directorygit add .
git commit -m "message"Commits changes with a messagegit commit -m "Initial commit"
git logShows the commit historygit log
git log --onelineShows a compact commit historygit log --oneline
git checkout -b <branch>Creates and switches to a new branchgit checkout -b feature-branch
git checkout <branch>Switches to an existing branchgit checkout main
git merge <branch>Merges a branch into the current onegit merge feature-branch
git push -u origin <branch>Pushes changes to the remote repositorygit push -u origin main
git pullFetches and merges changes from the remotegit pull
git stashTemporarily saves uncommitted changesgit stash
git stash applyApplies stashed changesgit stash apply
git remote -vLists remote repositoriesgit remote -v
git remote add <name> <url>Adds a remote repositorygit remote add origin https://github.com/user/repo.git
git remote rm <name>Removes a remote repositorygit remote rm origin
git fetch <remote>Fetches updates from the remotegit fetch origin
git rebase <branch>Reapplies commits on top of another branchgit rebase main
git reset --soft <commit>Resets HEAD to a previous commit, keeping changes stagedgit reset --soft HEAD~1
git reset --mixed <commit>Resets HEAD and unstages changesgit reset --mixed HEAD~1
git reset --hard <commit>Resets the working directory and indexgit reset --hard HEAD~1
git revert <commit>Reverts a specific commitgit revert <commit_hash>
git branch -d <branch>Deletes a branchgit branch -d feature-branch
git show <commit>Displays information about a commitgit show <commit_hash>
git stash popApplies and removes the latest stashgit stash pop
git log --graphShows a graph of commitsgit log --graph
git log --decorateShows commit history with referencesgit log --decorate
git tag <tag>Creates a lightweight taggit tag v1.0.0
git tag -a <tag> -m "message"Creates an annotated taggit tag -a v1.0.0 -m "Version 1.0"
git push origin --tagsPushes tags to the remote repositorygit push origin --tags
git rebase -i HEAD~nInteractively rebases the last n commitsgit rebase -i HEAD~3
git cherry-pick <commit>Applies a specific commit to the current branchgit cherry-pick <commit_hash>
git checkout <commit>Switches to a specific commitgit checkout <commit_hash>
git diff <commit1> <commit2>Shows differences between two commitsgit diff <commit1> <commit2>
git blame <file>Shows who last modified each line of a filegit blame myfile.txt
git bisectHelps find the commit that introduced a buggit bisect
git reflogShows a log of all reference updatesgit reflog
git cat-file -p <commit>Shows the content of a commit objectgit cat-file -p <commit_hash>
git rev-parse <ref>Retrieves the SHA-1 hash of a referencegit rev-parse HEAD
git fsckVerifies the integrity of the repositorygit fsck
git gcCleans up unnecessary files in the repositorygit gc
git submodule add <repo> <path>Adds a submodule to the repositorygit submodule add https://github.com/user/submodule.git
git submodule initInitializes submodulesgit submodule init
git submodule updateUpdates submodulesgit submodule update
git grep <pattern>Searches for a pattern in tracked filesgit grep "TODO"