DEV Community

Cover image for Git & GitHub: A Beginner’s Complete Guide
Tapoban Ray
Tapoban Ray

Posted on

Git & GitHub: A Beginner’s Complete Guide

In this article, you are going to learn the most basic GitHub commands that you will need in your everyday life as a developer. We will start by creating a repository on your local system and then proceed to creating and merging branches in GitHub.

Feel free to checkout my GitHub and follow me if you find it helpful.


Connecting your remote GitHub account to your local system

  1. Create GitHub account.
  2. Install Git on your system.
  3. Run the following SSH command. Learn more.
ssh-keygen -t ed25519 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Keep pressing "Enter" after that, and your ssh keys will be generated.

  1. Then open C:/Users/John/.ssh/id_ed25519.pub file and copy the entire text present in it.
  2. Then open your GitHub account > Settings > SSH and GPG keys. Click on New SSH Key. Learn more.
  3. Add your copied public ssh key here and save.
  4. Now, set the name, email and username by using the following commands on your local device.
git config --global user.email "Your email"
git config --global user.name "Your Name"
Enter fullscreen mode Exit fullscreen mode

Creating your first repository and connecting with remote GitHub

Create your local repository by running the following command. Learn More.:

# Create your repository with default branch 'master'
git init

# Create your repository with a custom branch name:
git init -b main
Enter fullscreen mode Exit fullscreen mode

Copy the SSH/https URL of your repository and run the following command inside your new repository folder:

git remote add origin your-repository-url
Enter fullscreen mode Exit fullscreen mode

View all the added remote origins:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Add, Commit and Push

# Add all your changes to staging area by running the following command:
git add . 
# OR 
git add filename1 filname2 ...

# Commit all your changes locally on your system.
git commit -m "commit message" 
# OR
git commit -m "commit message" -m "[OPTIONAL] Description of the commit"

# Publish all your changes remotely on GitHub.
git push origin main 
# OR
git push origin custom_branch_name
Enter fullscreen mode Exit fullscreen mode

Unstage changes
Learn more about Staging Area

# Discard changes done in working directory after last commit
git restore <file>

# Unstage changes.
git restore --staged <file>

# Reset all tracked files in the current directory to the last commit.
git reset .

# Reset the HEAD of our project by one commit
git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

Why and when to use git pull?

Suppose two persons, Person A and Person B are working on a repository from two different computers. Person A makes some changes in the codebase and pushes it to GitHub. Now to, Person B needs to update the code in their system with the new code, or else conflicts might appear. This process of updating the old code present in your local system with the new code present in the remote GitHub repository is accomplished by using the following command:

git pull origin main 
# OR
git pull origin custom_branch_name
Enter fullscreen mode Exit fullscreen mode

Concept of git pull:

It fetches the latest code from remote repository and then merges them into your current branch.
git pull = git fetch + git merge


What is .gitignore file?

Any file or folder that the developer doesn't want to commit and publish on GitHub needs to be listed here. Learn More.

Here's a short example of .gitignore file:

# Ignore node_modules
node_modules/

# Ignore secrets
.env

# Ignore cache folders
pycache/
Enter fullscreen mode Exit fullscreen mode

Check out the pre-designed gitignore templates from GitHub.


Create, edit, push, merge and delete branches

Learn more about Branching in Git

View current working branch and the list of branches present in your local system:

git branch
Enter fullscreen mode Exit fullscreen mode

Create a branch

git branch new_branch_name
Enter fullscreen mode Exit fullscreen mode

Switch to the new branch

git switch branch_name
Enter fullscreen mode Exit fullscreen mode

Publish your branch to remote:

git push origin new_branch_name
Enter fullscreen mode Exit fullscreen mode

Delete a branch from your local system:

# Delete fully merged branch locally
git branch -d branch_name

# Delete branch (even if not merged) locally
git branch -D branch_name

# Delete a branch from remote repository
git push origin --delete branch_name
Enter fullscreen mode Exit fullscreen mode

Fetch a specific branch to your local system

git fetch origin <branch_name>:<branch_name>
Enter fullscreen mode Exit fullscreen mode

Merge your branch into another branch

Suppose you want to merge your changes from branchA to branchB

  1. Checkout to branchB.
  2. Run the following command:
git merge branchA
Enter fullscreen mode Exit fullscreen mode

With this, you’ve covered the most essential Git commands. Once you have a good grasp on these commands and concepts, you can explore advanced concepts like rebasing, stashing, and resolving conflicts.

Top comments (0)