Basic Git Commands for Terminal
Tags: git, beginners, webdev
Published: 2022-02-18 21:04:17

## What is GitHub

> According to [Kinsta](https://kinsta.com/knowledgebase/what-is-github/) "GitHub is a for-profit company that offers a cloud-based Git repository hosting service. Essentially, it makes it a lot easier for individuals and teams to use Git for version control and collaboration."

GitHub is a very popular Version Control System that is capable of doing a lot of things. From storing repo's to hosting blogs and webpages, there is a lot you can do with GitHub. When I started my journey to becoming a software engineer, I had never heard of GitHub. However, after beginning my education, I use it almost daily. I started by using it only by explicitly following the directions provided in my coursework, but with time I was expected to know more and be able to use it fairly comfortably. I am by no means an expert and still have a lot to learn about Github, but I thought I would write about some of the basic Git commands I have learned on my journey.

---

## Useful Terms

![Repos](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wa7gnwltwrpoivb3eog6.png)
- <u>**repository** (or repo, for short)</u>: A directory of files that are tracked by Git.
- <u>**track**</u>: Track means that Git will record any changes to a file. These changes are often called differences or diffs. Git allows you to choose whether to commit a diff or not.
- <u>**diff**:</u> Any changes made to your file(s) since the last commit are known as diffs. Another term is the diffset, which refers to all the changes that have been made on all the files of a repo since its last commit.
- <u>**commit**:</u> A commit is like saving the changes you have made to your repo. To commit you need to write a message (usually a brief description of the changes made). You can track the commits to see what changes have been made.
- <u>**log**:</u> A record of what happened in each commit.
- <u>**local/remote**:</u> When forking from an existing repo, the original the remote. The repo on our personal system is referred to as the local repo.

- <u>**fork**:</u> Creating a copy of an existing repo (including all branches) to our own local repo.

![Branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q80jdb54evmm0ahc57d0.png)
- <u>**branch/default branch**:</u> A Git repo can support multiple branches that make it possible for multiple developers to be working on the code at the same time. When you initialize a new Git repo, a default branch is created where your work will be tracked by default. The name of the branch will often be main, but you will also see older repos that use master as the default branch.

---

## Basic Commands

- <u>**git init**:</u> This command initializes a new, empty repository. I can also be used to create a new Git repository. Without initializing the repo, you won't be able to use many other git commands, so this is usually done first.
- <u>**git add**:</u> This command adds a file to your repository. Write the command and then the name and type of file you want to create. This command also stages changes to your repo that will be stored in the commit.
- _Example:_ `$ git add style.css`
- <u>**git commit**:</u> Preforming a commit is usually done right after, or sometimes in conjunction with a git add command. It captures a snapshot of the project's currently staged changes. Commits will keep your code "safe" and stay the same unless you command to change it. 
- <u>**git pull**:</u> Git pull updates your current local branch and updates the remote tracking for all remote branches. It will fetch the new commits and merges from your remote GitHub repository and merges them to your local branch.
- <u>**git checkout**:</u> This command has to do with branches. It is capable of switching branches you are in. By writing `$ git checkout *branch name goes here*`, you will switch between branches. You can also use this command to create a new branch by adding a -b. `$git checkout -b *new branch name*`
- <u>**git fetch**:</u> This imports any changes made in your remote repo to your local repo.
- <u>**git push**:</u> This pushes changes up to your remote repository. It transfers commits from your local repo to the remote one. This is the opposite of git fetch, it takes your local changes and adds them to the remote repository. This command has the potential to overwrite changes, so be cautious when using this command.
- <u>**git merge**:</u> Git merge allows several branches to integrate into one branch. 
- <u>**git status**:</u> This checks status of your branch> It will let you know if there have been any commits or adds that need to be pushed up to the remote repo. It will tell you what has been staged, what hasn't, and what files are being tracked by git.
- <u>**git remote**:</u> This command lists the remotes available in our Git repo.
- <u>**git stash**:</u> git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.
- <u>**git diff**;</u> Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more. This document will discuss common invocations of git diff and diffing work flow patterns. The git diff command is often used along with git status and git log to analyze the current state of a Git repo.

## Conclusion
These are some of the most basic GitHub commands to use in your terminal while working on a project. There is much more you can do and a lot of minute details and applications for these basic Git commands.



[Source: Bitbucket Tutorials](https://www.atlassian.com/git/tutorials/setting-up-a-repository)

[Source: freeCodeCamp](https://www.freecodecamp.org/news/git-pull-explained/#:~:text=git%20pull%20is%20a%20Git,a%20repository%20from%20a%20remote.&text=By%20default%2C%20git%20pull%20does,branches%20for%20all%20other%20branches.)

Source: Flatiron School Prework Course | Git Module