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

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

On this Series
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
On this Post
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

1. Create a new Repository
To initialize a new git repository and to set up all the configuration files needed by git.
Use
git initto 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
2. Change the remote URL
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"
3. Pull latest changes from remote
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
4. Configure line endings
Configures the line endings for a repository.
Use
git config core.eol [lf | crlf]to configure the line endings.lfis the UNIX line ending (\n), whereascrlfis the DOS line ending (\r\n).
git config core.eol [lf | crlf]
# Example
git config core.eol lf # Configured to use UNIX line endings
5. View “undo” history
View git’s reference logs. This is especially useful for finding references that don’t show up in commit history.
Use
git reflogto 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
6. Reset the main to match the remote
Resets the local main branch to match the one on the remote.
Use
git fetch originto retrieve the latest updates from the remote.Use
git checkout mainto switch to themainbranch.Use
git reset --hard origin/mainto reset the localmainbranch to match the one on the remote.
git fetch origin
git checkout main
git reset --hard origin/main
7. Configure Git user information
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"
8. Optimize the local repository
Optimizes the local repository.
- Use
git gc --prune=now --aggressiveto garbage collect loose objects.
git gc --prune=now --aggressive
# Example
git gc --prune=now --aggressive # Optimizes the local repository

9. Find lost files
Prints a list of lost files and commits.
Use
git fsck --lost-foundto print a list of all dangling objects.All appropriate files will be extracted into the
.git/lost-founddirectory.
git fsck --lost-found
# Example
git fsck --lost-found
# dangling commit 3050fc0de
# dangling blob 807e3fa41
# dangling commit 59ff8481d
10. Disable fast forward merging by default
Disables the default fast forwarding on merge commits.
Use
git config --add merge.ff falseto disable fast-forward merging for all branches, even if it is possible.You can use the
--globalflag 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
11. View a visual graph of the repository
Prints a visual graph of all commits and branches in the repository.
Use
git log --pretty=oneline --graph --decorate --allto 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

12. Purge a file from the history
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 -- --allto rewrite the branch's history, passing it the previous command.You can optionally use
git push <remote> --force -allto 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
Congratulations!





