DEV Community

Cover image for Basic Git Commands for Terminal
krisperu
krisperu

Posted on • Updated on

Basic Git Commands for Terminal

What is GitHub

According to Kinsta "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

  • repository (or repo, for short): A directory of files that are tracked by Git.
  • track: 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.
  • diff: 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.
  • commit: 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.
  • log: A record of what happened in each commit.
  • local/remote: When forking from an existing repo, the original the remote. The repo on our personal system is referred to as the local repo.

  • fork: Creating a copy of an existing repo (including all branches) to our own local repo.

Branches

  • branch/default branch: 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

  • git init: 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.
  • git add: 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
  • git commit: 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.
  • git pull: 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.
  • git checkout: 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*
  • git fetch: This imports any changes made in your remote repo to your local repo.
  • git push: 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.
  • git merge: Git merge allows several branches to integrate into one branch.
  • git status: 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.
  • git remote: This command lists the remotes available in our Git repo.
  • git stash: 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.
  • git diff; 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

Source: freeCodeCamp

Source: Flatiron School Prework Course | Git Module

Top comments (5)

Collapse
 
hunghvu profile image
Hung Vu • Edited

I think it is better to call the title "Git commands", not "GitHub commands", GitHub has its own CLI, and the commands are very different compared to universal git commands.

Collapse
 
curiousdev profile image
CuriousDev

This is what I thought. The description here is about working with Git and not Github. The latter integrates Git and there are also other systems providing an integration, like Azure Repos.

Collapse
 
krisperu profile image
krisperu

Thanks! I have updated the name per your suggestion.

Collapse
 
pavloskl profile image
pavloskl

Nice article/cheat sheet for basic git commands.
I would be more precise with the "git fetch" command. You write:
git fetch: This imports any changes made in
your remote repo to your local repo.
While this is true, you should explain that git fetch does not integrate these new data into your working files so is a great way to get a fresh view on all the changes in the repository.
Therefore due to its harmless nature you are always sure that your working files are safe.

Collapse
 
krisperu profile image
krisperu

Thanks for the feedback, I will work on getting that changed.