Skip to content

Instantly share code, notes, and snippets.

@unrivaledcreations
Created October 14, 2021 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unrivaledcreations/2b1d9c2edc475ef173f368253d146ce1 to your computer and use it in GitHub Desktop.
Save unrivaledcreations/2b1d9c2edc475ef173f368253d146ce1 to your computer and use it in GitHub Desktop.
A beginner's Git cookbook, showing the most common Git commands one might use when working in a Git repository.

Beginner's Git Cookbook

Here are the most used and most common Git commands. This text assumes your primary branch is called main.

Command Effect
git status Will report the status of files changed on the local filesystem. Always check this status before messing around with checkouts, branches, pushes, pulls, etc.
git checkout -b {branch} Create, and switch over to a new branch; this is how you make a new branch to work in.
git checkout {branch} Goes to an existing branch; e.g., git checkout main.
git branch See a list of branches.
git merge {branch} Grab file changes from another branch and merge it into the current one; e.g., after git checkout main, you might do a git merge newfeature to merge the changes made in the newfeature branch back into the main branch.
git branch -d {branch} Deletes the specified branch; don't worry, Git won't let you lose any work by deleting any branch. Useful for getting rid of temporary branches have had a merge into the permanent main branch for housekeeping.
git commit -am {message} Takes a snapshot of your work, giving it a brief description (or "message") describing the changes.
git push Pushes your local files over to "origin", the Github repository.
git fetch Checks Github for changes; will not change files, it just lets you see if there's anything newer on Github that you need to pull.
git pull Grab the latest code from "origin" on Github, back down to your computer.

Example Workflow

  1. $ git status (...make sure the working tree is clean...)
  2. $ git checkout -b newfeature (...creates a new branch...)
  3. $ git commit -am 'Added a cool new feature' (...creates a snapshot of your files after you make some changes; do this as often as you want, multiple snapshots can only be helpful if you have to go back to find problems...)
  4. $ git checkout main (...switches the files in your working directory back over to the permanent "main" branch...)
  5. $ git merge newfeature (...imports the changes you made in the temporary "newfeature" branch, to make them a part of the permanent "main" branch...)
  6. $ git branch -d newfeature (...finally, a little housekeeping; deletes the now-unneeded temporary "newfeature" branch...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment