Basics of the Git Workflow for a Code Newbie
Tags: git, codenewbie, programming, tutorial
Published: 2021-09-01 06:45:05

Last month, I read up on the [Git version control system](https://git-scm.com/book/en/v2). I actually found it difficult to understand initially, but after reading several articles on Git, watched a lot of YouTube videos, and completed my [The Odin Project lesson](https://www.theodinproject.com/paths/foundations/courses/foundations/lessons/git-basics), I think I have a clear idea now. Here is a summary of the git commands I learned, as a cheatsheet for future use by me - and any other fellow Git-beginners out there. But before we dive into all that, let's address this obvious question;
 
## ***What Exactly is Git?***

Git is a free and open-source distributed version control system. 

[Version control](https://en.wikipedia.org/wiki/Version_control) systems (also known as revision control, source control, or source code management) are used to manage changes to [computer programs](https://en.wikipedia.org/wiki/Computer_program), documents, large [web sites](https://en.wikipedia.org/wiki/Website), and other collections of information. These systems provide an automatic way to track changes in projects thereby giving creators the power to view previous versions of [files](https://en.wikipedia.org/wiki/Computer_file) and [directories](https://en.wikipedia.org/wiki/Directory_(computing)), securely back up the project and its history, and collaborate easily and conveniently with others. They are crucial for tracking edits made by others, correcting errors, and protecting against spam and vandalism. Distributed version control (also known as distributed revision control) is a form of version control in which the complete [codebase](https://en.wikipedia.org/wiki/Codebase), including its full history, is mirrored on every developer's computer. 

Git is acessed via a [command-line program](http://www.linfo.org/command_line_program.html) called `git` which lets users transform an ordinary directory into a [*repository*](https://en.wikipedia.org/wiki/Software_repository) (a sort of enhanced directory with the additional ability to track changes to every file and sub-directory). Here are some basic commands frequently used when running Git 👇

## **Git Cheatsheet**

All Git commands consist of the command-line program `git` followed by the name of the command, so for the full command to:

- to check the version of git installed on your system, type 👉 `git --version`.


- to set your global configuration settings (i.e. your git name and username), type 👉 `git config --global user.name <yourname>` and `git config --global user.email <email address>`. To configure your local default git branch name to *main* (rather than *master* which has always been used), type 👉 `git config --global init.defaultBranch main`. To check all your git configuration values, type 👉 `git config --list`.


- to initialize a git repository, type 👉 `git init`. This creates a special hidden directory called .git where git stores the information it needs to track your project’s changes. (It’s the presence of a properly configured `.git` directory that distinguishes a git repository from a regular directory.)


- to clone a remote repository on your computer, type 👉 `git clone <URL where the repository is>`. To create a file in a git repository just use `touch` like you would when creating a new file on the command line. To check if you've successfully connected your remote repository to your local machine, navigate to the cloned repository and confirm its URL by typing 👉 `git remote -v`. To change the URL of your git remote origin, type 👉 `git remote set-url origin <newurl>`


- to hide files in your repository that you don't want others to see, create a git ignore file using touch, type 👉 `touch .gitignore` and manually include the names of those files (or file patterns/extensions) you want ignored to the .gitignore text file.


- to check the status of your repository, use 👉 `git status`. This enables you to see all *changes* to files within the respository, displayed to show; files that are merely in your working directory (these are termed 'untracked files'), files that have been edited but not yet (re)staged for commit and files in the staging area (with *changes* about to be committed to the repository).


- to make a commit in git: first add the file (or files) you want to commit to the staging area, type 👉 `git add <filename>` to add a single file or `git add .` to add all unstaged files *in the current directory* and `git add -A` to add all untracked files and directories *in the repository*. Then type 👉 `git commit -m <a message describing what you have done to make this snapshot different>` to commit your changes. 


- to remove a single file from the staging area, type 👉 `git reset <filename>` or `git restore --staged <filename>` or `git rm --cached <filename>`. To remove all files, simply type 👉 `git reset`. To check the difference between the last commit and unstaged changes in your current project, type 👉 `git diff`.


- to upload/push your work to a remote repository, type 👉 `git push origin main`.  To see the history of your commits, type 👉 `git log`. This command shows only the commits messages which isn't particularyly detailed. To see the full diffs representated by each commit, use the `-p` flag, type 👉 `git commit -p`


- to commit *all changes* in your currently existing files, pass the `-a` flag to the `git commit` command by typing 👉 `git commit -am <message>`. The `-a` flag commits *all pending changes* to files in the repository. Always remember to type 👉 `git add -A` when there are new files.


- to see the difference between staged changes and the previous version of the repo, type 👉 `git diff --staged`.


- to correct an error in /update your last commit message, type 👉 `git commit --amend`. To ascertain that your git commit message was properly updated, run `git log` to  get the SHA of your last commit then view the diff using 👉 `git show <SHA>`.

### **Resources** 

I generally use the [The Odin Project](https://www.theodinproject.com) curriculum to study. In addition to that, other resources I found useful while reading up on Git include:

1. [Learn Enough Git to be Dangerous](https://www.learnenough.com/git-tutorial)
2. Corey Schafer's [Git Tutorial for Beginners](https://www.youtube.com/watch?v=HVsySz-h9r4) on Youtube
3. Fireship's Youtube video on [Git It? How to use Git and Github](https://www.youtube.com/watch?v=HkdAHXoRtos)

Hey, thanks for reading!  👋 👋