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
# Set username and email, for the current project
$ git config user.name <YOUR_USERNAME>
$ git config user.email <YOUR_EMAIL>
# Check your username and email, for all the projects in the device
$ git config --global user.name
$ git config --global user.email
# 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>
Add Git to your project:
$ git init
Remote:
- List remote branches:
$ git remote -v
- Remote public:
$ git remote add "<REMOTE_NAME>” <REPO_URL>
# ----- OR
$ git remote add origin <REPO_URL>
- 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>
- Remove remote:
# Removing current remote and set another one
$ git remote set-url origin git://<NEW_URL_HERE>
# Just remove remote
$ git remote remove origin
# ----- OR
$ git remote remove <REMOTE_NAME>
Add files for commit/push/stash…:
- All files:
$ git add .
- Single file:
$ git add <FILE_NAME>
Make a commit:
$ git commit -m “<YOUR_COMMIT>”
Check the changes:
$ git status
Check commit logs:
$ git log
How to Push to an existing repository (GitHub):
# “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
How to Create a New Repository and Push the project (GitHub):
- Open GitHub and create a new repo from “Repositories”:
- Open your project that you want to push it
- Right click on empty place and open “Git Bash Here”
$ 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
Clone:
- Clone public repo:
$ git clone <REPO_URL>
- 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
PUSH:
# Push the commits
$ git push
PULL:
# “git pull” is a combination of “git fetch” and “git merge”
$ git pull
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>
List current commit history:
$ git reflog
Undo commit:
- Undo last commit:
$ git reset --soft HEAD~1
# OR
$ git revert HEAD
- Undo to specific commit:
$ git reflog
$ git revert <ID>
List current commit history:
$ git reflog
$ git revert <ID>
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
Merge:
$ git merge <BRANCH_NAME>
Count Commits:
$ git shortlog -s
$ git shortlog -s -n
More:
GitHub: Git-Guides
My GitHub: Ammar7077
Top comments (0)