DEV Community

julie
julie

Posted on • Updated on

Everything I Know So Far About Git/GitHub...for now

This is just the very basics of Git/GitHub as I am still learning the ins and outs of this complex tool!

Introduction
Git is a version control system that helps manage changes made to files and directories in a project. They allow you to keep track of what you did when, undo any changes you decided you don't want, and collaborate with others.

GitHub is a web-based hosting service for git repositories.

Basically, it is like when working on a Google Docs where you have multiple people making edits to a document and you can see the edit histories and possibly revert to an old version.

Basic Terminologies

  • Local repository(repo): a local directory containing code and files for the project. (aka your personal computer)
  • Remote repo: an online version of the local repository hosted on services like GitHub
  • Cloning: making a clone or copy of a repository in a new directory
  • Commit: a snapshot of the project you can come back to -Branch: a copy of the project used for working in an isolated environment without affecting the main project
  • Git merge: the process of combining two branches

Installation and Set Up
I don't feel well-versed enough to explain the installation process so I've attached a link on how to install and set up: Link

Git Basic Commands (These are my everyday commands)
1.) To create a local repo, clone a repo from a remote host(GitHub):
git clone <the_remote_repo_url>
(Note: you will need to fork a repo on GitHub to get the URL. Forking is getting your own copy of the repo)

Just click on the fork button and it will take you to another page, click create fork and then copy that url.

Image description

2.) See the current status of your local repository:
git status

3.) Create a branch:
git branch <name_of_branch>
(Note: 'git branch' can show you which branch you are currently on)

4.) Switch into an existing branch named :
git checkout <new_branch>

5.) Add all untracked/tracked files inside the current directory to git:
git add .

6.) Saving a snapshot of the staged changes with a custom message:
git commit -m "Commit message"
(Note: be careful of what you write for the message because it will be difficult to revise the message in the future)

7.) Saving staged/unstaged changes to stash for later use:
git stash
(Note: stash is great for when you want to save the edit but are not ready to commit the changes. You put them off to the side.)

8.) List remote repos in details:
git remote -v

9.) Push a copy of the local branch named branch to the remote repo:
git push <remote_repo> branch

For example:

(learn-env) julie@Julie ~ % git push origin main
Enter fullscreen mode Exit fullscreen mode

Here, I am taking all the changes I made on my local repo (my computer) and "pushing" them onto my remote repo (in GitHub) which is called origin, and specifically onto my main branch.

10.) Pull a copy from remote branch named branch to the local repo:
git pull <local_repo> branch

For example,

(learn-env) julie@Julie ~ % git pull upstream main
Enter fullscreen mode Exit fullscreen mode

So, same as the previous example. Here, we are “pulling” new updates from a remote repo (from GitHub) and syncing the changes to my local computer. Upstream is the name of the remote repo and main is the name of the branch you are taking the updates from.

Avoiding a merge conflict
Luckily, I have not experienced a merge conflict (fingers crossed). The only advice I can offer right now to avoid a merge conflict is to communicate with your collaborators when you are adding/committing changes to the repo. It always comes back to communication.

More Resources
This is just the tip of the iceberg with the commands.
Here's a list of some of the more commonly used commands for Git/terminal:
More commands

P.S. I will be continuously expanding on this list as I learn more features of Git/GitHub! :)

Top comments (0)