THE Git Terminal Cheat Sheet
Philip John Basile

Philip John Basile @philipjohnbasile

About: Staff Full Stack Engineer | Real-Time 3D & Game Development | VR/AR | AI-Driven Experiences

Location:
New Rochelle, New York
Joined:
Oct 2, 2019

THE Git Terminal Cheat Sheet

Publish Date: Jun 28 '23
40 7

Version Control in General, Why?

  • merges files when there is more than one person working on the same file.

  • acts like a time capsule where you can check past commits if you ever need to go back.

Why is GIT better?

  • It's a distributed repo. Everyone has a copy on their machines.
  • It's not in a central repository.
  • You can work on it offline.

git config - global user.name "Philip John Basile"

Enter fullscreen mode Exit fullscreen mode

Setting the author of the code


git config - global user.email name@email.com

Enter fullscreen mode Exit fullscreen mode

Setting the author's email


git config - global color.ui true

Enter fullscreen mode Exit fullscreen mode

turn on pretty colors


git init

Enter fullscreen mode Exit fullscreen mode

Initializes a Git repo in your chosen directory.


git status

Enter fullscreen mode Exit fullscreen mode

Sees what the current state of our project is and what has changed.


git commit -m 'add cute octocat story'

Enter fullscreen mode Exit fullscreen mode

Stores the saved changes with a message describing what was changed.


git add octocat.txt

Enter fullscreen mode Exit fullscreen mode

Tells Git to start tracking changes made to octocat.txt


git add README.txt LICENSE

Enter fullscreen mode Exit fullscreen mode

adds the two files README.txt and LICENSE to the staging area.


git add *.txt

Enter fullscreen mode Exit fullscreen mode

Adds all txt files in current directory


git add docs/*.txt

Enter fullscreen mode Exit fullscreen mode

Adds all txt files in docs directory


git add docs/

Enter fullscreen mode Exit fullscreen mode

Adds all files in docs directory


git add '*.txt'

Enter fullscreen mode Exit fullscreen mode

Adds all txt files in the whole project


git add - all

Enter fullscreen mode Exit fullscreen mode

adds all files to the staging area.


git log

Enter fullscreen mode Exit fullscreen mode

Git's journal that remembers all the changes we've committed so far in the order we committed them.


git remote add origin https://github.com/try-git/try_git.git

Enter fullscreen mode Exit fullscreen mode

Pushes our local repo to the remote GitHub server.


git push -u origin master

Enter fullscreen mode Exit fullscreen mode

The push command tells Git where to put our local commits when we're ready. The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do.


git pull origin master

Enter fullscreen mode Exit fullscreen mode

Checks for changes on our GitHub repo and pulls down any new changes.


git diff HEAD

Enter fullscreen mode Exit fullscreen mode

Views all of the merge conflicts. Since HEAD is used it diffs against our most recent commit.


git diff - staged

Enter fullscreen mode Exit fullscreen mode

Lets you see the changes that were just staged.


git reset octofamily/octodog.txt

Enter fullscreen mode Exit fullscreen mode

Lets you unstage files using the git reset command.


git checkout - octocat.txt

Enter fullscreen mode Exit fullscreen mode

If you mess up, you can replace the changes in your working tree with the last content in head. Changes already added to the index, as well as new files, will be kept. This gets rid of all the changes since the last commit for octocat.txt.


git branch clean_up

Enter fullscreen mode Exit fullscreen mode

Creates a copy of their code they can make separate commits to. Once done you merge this branch into the main master branch.


git branch

Enter fullscreen mode Exit fullscreen mode

Lists the local branch(es).


git checkout clean_up

Enter fullscreen mode Exit fullscreen mode

Switch from one branch to another.


git rm '*.txt'

Enter fullscreen mode Exit fullscreen mode

This command will not only remove the actual files from disk, but will also stage the removal of the files for us.


git merge clean_up

Enter fullscreen mode Exit fullscreen mode

Merges the changes from the clean_up branch into the master branch.


git branch -d clean_up

Enter fullscreen mode Exit fullscreen mode

Delete a branch when you have merged its contents and do not need it anymore.


git push

Enter fullscreen mode Exit fullscreen mode

Moves content to the remote repo.

If you enjoy my technology-focused articles and insights and wish to support my work, feel free to visit my Ko-fi page at https://ko-fi.com/philipjohnbasile. Every coffee you buy me helps keep the tech wisdom flowing and allows me to continue sharing valuable content with our community. Your support is greatly appreciated!

Comments 7 total

  • Tia Eastwood
    Tia EastwoodJun 28, 2023

    Useful!

  • SilverShade
    SilverShadeJun 29, 2023

    We're supposed to add files to tracking everytime we commit right?

    • Philip John Basile
      Philip John BasileJul 1, 2023

      If you want those files tracked. Things like the node modules folder or notes should not be in there. And no api keys if it’s on a public git.

    • Jesse Phillips
      Jesse PhillipsJul 3, 2023

      add will put changes in your stage which is used to form a commit. New files will track, but the main operation isn't to track a file.

  • redbilledpanda
    redbilledpandaJun 30, 2023

    how is git merge <branch-name> different from git rebase <branch-name> ? Will the merge just merge the files without creating a commit?

    • Bao DK
      Bao DKJul 1, 2023

      merge will merge current branch to master. rebase will apply master onto the current.

    • Jesse Phillips
      Jesse PhillipsJul 3, 2023

      The odd on out is rebase. A branch is a deviation from some commit. A base is the point the deviation occurred. To rebase is to change the commit where the branch starts to deviate. The short form is to rebase is where each change is reapplied (cherry-picked) an a new location.

Add comment