Git for Noob-to-Pro — #1(Repository)

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this series. Find the quick read 👇

We are going to see the commonly used Git snippets for the repository which will make you from Noob to Pro.
The Git Repository snippets that we are going to look in this series are
Local Repository
Branches
Commits
Module
Stash
In this post, we are going to see the topic Local Repository which has the following sub-topics like
Create a new Repository
Change the remote URL
Pull latest changes from remote
Configure line endings
View “undo” history
Reset main to match remote
Configure Git user information
Optimize the local repository
Find lost files
Disable fast forward merging by default
View a visual graph of the repository
Purge a file from the history

To initialize a new git repository and to set up all the configuration files needed by git.
Use git init to initialize an empty repository in the current directory.
Alternatively, use git init [<directory>] it to initialize the repository in the specified <directory>.

git init [<directory>]
# Example
cd ~/my_project
git init # Initializes a repo in ~/my_project
cd ~
git init my_project # Initializes a repo in ~/my_project
Changes the URL of the remote repository.
Use git remote set-url origin <url> to change the URL of the remote repository to <url>.
git remote set-url origin
# Example
git remote set-url origin https://github.com/nidhinkumar06/HTML-LayoutPatterns.git
# The remote URL is now "https://github.com/nidhinkumar06/HTML-LayoutPatterns.git"
Pulls the latest changes from the remote tracking branch.
Use **git pull**to fetch and apply the latest changes from the remote.
git pull
# Example
# Assuming the remote `patch-1` branch is ahead of the local one
git checkout patch-1
git pull # The local `patch-1` branch is now up to date with the remote branch
Configures the line endings for a repository.
Use git config core.eol [lf | crlf] to configure the line endings.
lf is the UNIX line ending (\n), whereas crlf is the DOS line ending (\r\n).
git config core.eol [lf | crlf]
# Example
git config core.eol lf # Configured to use UNIX line endings
View git’s reference logs. This is especially useful for finding references that don’t show up in commit history.
Use git reflog to display the git reference log.
View your “undo” history
Sometimes git log doesn’t cut it, especially for commands that don’t show up in your commit history.
reflog is your safety net after running “scary” commands like git-rebase. You’ll be able to see not only the commits you made but each of the actions that led you there.
git reflog

Reflog Example
Resets the local main branch to match the one on the remote.
Use git fetch origin to retrieve the latest updates from the remote.
Use git checkout main to switch to the main branch.
Use git reset --hard origin/main to reset the local main branch to match the one on the remote.
git fetch origin
git checkout main
git reset --hard origin/main
Configures user information for git.
Use git config user.email <email> to set the user’s email for the current repository.
Use git config user.name <name> to set the user’s name for the current repository.
You can use the — global flag to configure global user information.
git config [--global] user.email
git config [--global] user.name
# Example
git config user.email "useremail@email.com"
git config user.name "User Name"
Optimizes the local repository.
git gc --prune=now --aggressive to garbage collect loose objects.git gc --prune=now --aggressive
# Example
git gc --prune=now --aggressive # Optimizes the local repository

Prints a list of lost files and commits.
Use git fsck --lost-found to print a list of all dangling objects.
All appropriate files will be extracted into the .git/lost-found directory.
git fsck --lost-found
# Example
git fsck --lost-found
# dangling commit 3050fc0de
# dangling blob 807e3fa41
# dangling commit 59ff8481d
Disables the default fast forwarding on merge commits.
Use git config --add merge.ff false to disable fast-forward merging for all branches, even if it is possible.
You can use the --global flag to configure this option globally.
git config [--global] --add merge.ff false
# Example
git config --global --add merge.ff false
git checkout main
git merge my-branch
# Will never fast forward even if it's possible
Prints a visual graph of all commits and branches in the repository.
Use git log --pretty=oneline --graph --decorate --all to view a visual graph of the whole repository's history.
Use arrow keys to navigate, and press Q to exit.
git log --pretty=oneline --graph --decorate --all

Completely purges a file from history.
Use git rm --cached --ignore-unmatch <path> to delete the file in the specified <path>.
Use git filter-branch --force --index-filter <command> --prune-empty --tag-name-filter cat -- --all to rewrite the branch's history, passing it the previous command.
You can optionally use git push <remote> --force -all to force push the changes to the remote repository.

git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch " \
--prune-empty --tag-name-filter cat -- --all
git push --force --all
# Example
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch config/apiKeys.json" \
--prune-empty --tag-name-filter cat -- --all
# Purges `config/apiKeys.json` from history
git push origin --force --all
# Force pushes the changes to the remote repository
