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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055968904/842f5ff0-e072-4395-bbb5-5be8ce4e4e5e.gif align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055971094/ab53c99c-e038-413e-9d73-c23a1e14e223.png align="left")

### 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>`.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055972772/bd1aa9b7-7ef6-41b4-bd47-1e5aca951ffc.png align="left")

```bash
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>`.

```bash
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.

```bash
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`).
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055975452/64f783c0-a19c-4ff4-908d-249cf0400dba.png align="left")

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.
    

```bash
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.

```bash
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.
    

```bash
git gc --prune=now --aggressive

# Example

git gc --prune=now --aggressive # Optimizes the local repository
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055977084/8e7509e3-4113-40c5-b641-281a709bde7e.png align="left")

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

```bash
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.
    

```bash
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.
    

```bash
git log --pretty=oneline --graph --decorate --all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055979151/d767f409-b9d3-4033-ba17-ed96ca10d1e5.png align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055980747/48628dfd-18fb-4d68-9431-5b8d5f749131.png align="center")

```bash
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!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055982627/10e02217-8309-4ad7-9bf7-a8881e9fa620.gif align="left")
