DEV Community

Cover image for All You Need to Know: Popular Tools for Git and GitHub
Ammar Omari
Ammar Omari

Posted on

All You Need to Know: Popular Tools for Git and GitHub

The provided content is a detailed guide that covers various aspects of using Git for version control. It includes instructions for setting up user configurations, initializing a Git repository, managing remotes, adding files, making commits, checking changes, viewing commit logs, pushing to existing repositories, creating new repositories, cloning, pulling, branching, undoing commits, stashing changes, merging branches, and counting commits.

This guide is likely to be helpful for users who are new to Git and GitHub or those looking for a comprehensive reference for common Git commands and workflows. It provides step-by-step instructions along with explanations for each command, making it accessible for users at different skill levels.

If the goal is to help users employ version control effectively, this guide can be a valuable resource as it covers a wide range of Git-related tasks commonly encountered in software development projects. The inclusion of links to additional resources at the end adds to the completeness of the guide.


GIT Download url: Git-Downloads


Username and Email:

# Check your username and email, for the current project
$ git config user.name
$ git config user.email
Enter fullscreen mode Exit fullscreen mode
# Set username and email, for the current project
$ git config user.name <YOUR_USERNAME>
$ git config user.email <YOUR_EMAIL>
Enter fullscreen mode Exit fullscreen mode
# Check your username and email, for all the projects in the device
$ git config --global user.name
$ git config --global user.email
Enter fullscreen mode Exit fullscreen mode
# Set username and email global, for all the projects in the device
$ git config --global user.name <YOUR_USERNAME>
$ git config --global user.email <YOUR_EMAIL>
Enter fullscreen mode Exit fullscreen mode

Add Git to your project:

$ git init
Enter fullscreen mode Exit fullscreen mode

Remote:

  • List remote branches:
$ git remote -v
Enter fullscreen mode Exit fullscreen mode
  • Remote public:
$ git remote add "<REMOTE_NAME>” <REPO_URL>

# ----- OR

$ git remote add origin <REPO_URL>
Enter fullscreen mode Exit fullscreen mode
  • Remote a repo/private repo: Open GitHub Account → Settings → Developer Settings → Personal access tokens - Tokens (classic) → Generate new token (classic)
# it will remove current remote and set another one
$ git remote set-url origin https://<token>@github.com/<username>/<repo>
Enter fullscreen mode Exit fullscreen mode
  • Remove remote:
# Removing current remote and set another one
$ git remote set-url origin git://<NEW_URL_HERE>
Enter fullscreen mode Exit fullscreen mode
# Just remove remote
$ git remote remove origin

# ----- OR

$ git remote remove <REMOTE_NAME>
Enter fullscreen mode Exit fullscreen mode

Add files for commit/push/stash…:

  • All files:
$ git add .
Enter fullscreen mode Exit fullscreen mode
  • Single file:
$ git add <FILE_NAME>
Enter fullscreen mode Exit fullscreen mode

Make a commit:

$ git commit -m “<YOUR_COMMIT>”
Enter fullscreen mode Exit fullscreen mode

Check the changes:

$ git status
Enter fullscreen mode Exit fullscreen mode

Check commit logs:

$ git log
Enter fullscreen mode Exit fullscreen mode

How to Push to an existing repository (GitHub):
Image description

# “git init” no need: if you have a git folder in your project … file named “.get”
$ git init 
$ git remote add origin git@github.com:<USERNAME>/<REPO_NAME>.git
$ git add .
$ git commit -m "<YOUR_COMMIT>"
$ git push -u origin main

# ------- OR

$ git remote add origin git@github.com:<USERNAME>/<REPO_NAME>.git
$ git add .
$ git commit -m "<YOUR_COMMIT>"
$ git push -u origin main
Enter fullscreen mode Exit fullscreen mode

How to Create a New Repository and Push the project (GitHub):

  • Open GitHub and create a new repo from “Repositories”: Image description

Image description

  • Open your project that you want to push it
  • Right click on empty place and open “Git Bash Here”

Image description

$ git init
# change from master branch to main branch
$ git branch -M main
$ touch README.md # optional
$ git add README.md # optional
$ git add .
$ git commit -m "first commit"
$ git remote add origin git@github.com:<USERNAME>/<REPO_NAME>.git
$ git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Clone:

  • Clone public repo:
$ git clone <REPO_URL>
Enter fullscreen mode Exit fullscreen mode
  • Clone Private repo: Open GitHub Account → Settings → Developer Settings → Personal access tokens - Tokens (classic) → Generate new token (classic)
$ git clone https://<TOKEN>@github.com/<USERNAME>/<REPO_NAME>.git
Enter fullscreen mode Exit fullscreen mode

PUSH:

# Push the commits
$ git push
Enter fullscreen mode Exit fullscreen mode

PULL:

# “git pull” is a combination of “git fetch” and “git merge”
$ git pull
Enter fullscreen mode Exit fullscreen mode

Branch:

# List local branches
$ git branch

# List all branches local and remote, (the connected branches = Red color)
$ git branch -a

# Create a new local branch
$ git branch <BRANCH_NAME>

# Delete a local branch (if merged)
$ git branch -d <BRANCH_NAME>

# Delete local branch (force, regardless of merge status), even if it’s merged or not
$ git branch -D <BRANCH_NAME>

# Move from the current branch to the existing branch
$ git checkout <EXISTING_BRANCH_NAME>

# Create and switch to a new local branch: Take the changes from the current branch and move it to the new branch
$ git checkout -b <NEW_BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

List current commit history:

$ git reflog
Enter fullscreen mode Exit fullscreen mode

Undo commit:

  • Undo last commit:
$ git reset --soft HEAD~1
# OR
$ git revert HEAD
Enter fullscreen mode Exit fullscreen mode
  • Undo to specific commit:
$ git reflog
$ git revert <ID>
Enter fullscreen mode Exit fullscreen mode

List current commit history:

$ git reflog
$ git revert <ID>
Enter fullscreen mode Exit fullscreen mode

Stash:

# List of stashes:
$ git stash list

# Add to stash:
$ git stash

# Apply specific stash index
$ git stash apply stash@{<INDEX>}

# Retrieve last stash Recommended:
$ git stash apply

# Retrieve last stash NOT recommended:
$ git stash pop

# Retrieve a specific stash:
$ git stash apply stash@{<INDEX>}

# Remove a single stashed state from the stash list:
$ git stash drop stash@{<INDEX>}

# Remove last stash:
$ git stash drop

# Remove all stashed files at once:
$ git stash clear
Enter fullscreen mode Exit fullscreen mode

Merge:

$ git merge <BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

Count Commits:

$ git shortlog -s
$ git shortlog -s -n
Enter fullscreen mode Exit fullscreen mode

More:
GitHub: Git-Guides

My GitHub: Ammar7077

Top comments (0)