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

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

  1. Local Repository

  2. Branches

  3. Commits

  4. Module

  5. Stash

On this Post

In this post, we are going to see the topic Local Repository which has the following sub-topics like

  1. Create a new Repository

  2. Change the remote URL

  3. Pull latest changes from remote

  4. Configure line endings

  5. View “undo” history

  6. Reset main to match remote

  7. Configure Git user information

  8. Optimize the local repository

  9. Find lost files

  10. Disable fast forward merging by default

  11. View a visual graph of the repository

  12. 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 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

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.

  • 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

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 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

6. Reset the main to match the remote

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

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 --aggressive to 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-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

10. Disable fast forward merging by default

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

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 --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

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 -- --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

Congratulations!