Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
goel4ever committed Jun 4, 2024
1 parent c2dc370 commit 85f96fd
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 54 deletions.
127 changes: 85 additions & 42 deletions src/Local/git-cli/cheatsheet.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,100 @@
# Git Cheatsheet

##### Commands - Git Basics
## Git Basics

```
$ git --version // Displays version of Git installed
$ git init // Initialize the current folder as a git repository
$ git init <directory> // Initialize the directory specified as a git repository
$ git clone <repo-url/path> // Clone an existing remote repo or local repo onto local machine
$ git status // List which files are staged, unstaged and untracked
$ git add <directory> // Stage all changes in directory for the next commit
$ git commit -m "<message>" // Commit the staged snapshot with <message> as commit message
$ git commit --amend // Update last commit message
$ git log // Display the entire commit history using default format. (customize)
$ git diff // Show unstaged changes between your index and working directory.
$ git diff HEAD // Show difference between working directory and last commit
$ git diff --cached // Show difference between staged changes and last commit
```
```sh
# Displays version of Git installed
$ git --version

##### Commands - Undoing Changes
# Initialize the current folder or directory as a git repository
$ git init
$ git init <directory>

```
$ git revert <commit> // Create new commit that undoes all of the changes made in <commit>, then apply
$ git reset // Reset staging area to match most recent commit, but leave code unchanged
$ git reset <file> // Remove <file> from staging area
$ git reset --hard // Reset staging area and working directory
$ git reset <commit> // Move current branch tip backward to <commit>, reset staging area but NOT local code
$ git reset --hard <commit> // Move current branch tip backward to <commit>, reset staging area as well as local code
$ git clean -n // ------- read more
```
# Clone an existing remote repo or local repo onto local machine
$ git clone <repo-url/path>

##### Commands - Git Branches
# List which files are staged, unstaged and untracked
$ git status

```
$ git branch // List all of the branches in your repo
$ git checkout <branch> // Checkout an existing branch named <branch>
$ git checkout -b <branch> // Create and checkout a new branch named <branch>
$ git merge <branch> // Merge <branch> into current branch
```
# Stage all changes in directory for the next commit
# Will only add new files and modified files to the index to be committed
$ git add <directory>
# Stage all changes in the repository
$ git add -A

##### Commands - Remote Repositories
# Commit the staged snapshot with <message> as commit message
$ git commit -m "<message>"

# Update last commit message
$ git commit --amend

# Display the entire commit history using default format.
$ git log

# Show unstaged changes between your index and working directory
$ git diff
# Show difference between working directory and last commit
$ git diff HEAD
# Show difference between staged changes and last commit
$ git diff --cached
```
$ git remote add <name> <url> // Create a new connection to remote repo. <name> is shortcut for <url>
$ git remote set-url <name> <url> // Update the remote repo url to <url> with shortcut <name>
$ git fetch <remote> <branch> // Fetches a specific <branch> from the remote repo.
$ git pull <remote> // Fetch the specified remote's copy of current branch and merge into local copy
$ git push <remote> <branch> // Push the <branch> to <remote>, along with necessary commits and objects.
// Creates named branch in remote if doesn't exist

## Undoing Changes

```sh
# Create new commit that undoes all of the changes made in <commit>, then apply
$ git revert <commit>

# Reset staging area to match most recent commit, but leave code unchanged
$ git reset
# Remove <file> from staging area
$ git reset <file>
# Reset staging area and working directory
$ git reset --hard
# Move current branch tip backward to <commit>, reset staging area but NOT local code
$ git reset <commit>
# Move current branch tip backward to <commit>, reset staging area as well as local code
$ git reset --hard <commit>

# Remove untracked files from your working directory
$ git clean
# Perform a "dry run" of the git clean
# Shows what files would be removed from the working directory, but it won't actually remove them.
$ git clean -n
```

##### Note
## Git Branches

```sh
# List all of the branches in your repo
$ git branch

# Checkout an existing branch named <branch>
$ git checkout <branch>

# Create and checkout a new branch named <branch>
$ git checkout -b <branch>

# Merge <branch> into current branch
$ git merge <branch>
```
$ git add . // Will only add new files and modified files to the index to be committed
$ git add -A // Will do everything + also schedule removed files for deletion

## Remote Repositories

```sh
# Create a new connection to remote repo. <name> is shortcut for <url>
$ git remote add <name> <url>

# Update the remote repo url to <url> with shortcut <name>
$ git remote set-url <name> <url>

# Fetches a specific <branch> from the remote repo.
$ git fetch <remote> <branch>

# Fetch the specified remote's copy of current branch and merge into local copy
$ git pull <remote>

# Push the <branch> to <remote>, along with necessary commits and objects
$ git push <remote> <branch>
#Creates named branch in remote if doesn't exist
```
22 changes: 21 additions & 1 deletion src/Local/git-cli/git-fetch-vs-pull.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# Git Fetch vs Pull

The `git pull` command is a combination of two other Git commands: `git fetch` and `git merge`. It fetches changes from the remote repository and automatically merges them into the current branch.
`git fetch` is used to retrieve the latest changes from the remote repository, but it does not merge them with your local branch. This command fetches all the branches from the remote repository, along with all the commits and files that have been updated. However, it does not modify your local working branch. After fetching, you can inspect the changes and decide whether to merge them into your local branch.

The `git pull` command is a combination of two other Git commands: `git fetch` and `git merge`. It fetches changes from the remote repository and automatically merges them into the current local branch.

## Usage

```sh
# Fetch all the changes from the origin remote repository
git fetch origin

# Fetch and merge all the changes from the origin remote repository
git pull origin main
```

## Storage

The fetched changes are stored in a separate branch in your local repository. This branch is usually a remote-tracking branch, which tracks the state of the remote repository. For example, if your current branch is `main`, it'll store the retrieved changes in the `origin/main` branch in your local repository. This `origin/main` branch is a remote-tracking branch for the `main` branch on the `origin` remote.

You can view these changes by checking out the remote-tracking branch with `git checkout origin/main`, or you can compare the changes with your local branch using `git diff main origin/main`.

Remember, these changes are not merged into your local `main` branch until you run `git merge origin/main` or `git pull origin main`.

## References

Expand Down
42 changes: 41 additions & 1 deletion src/Local/git-cli/git-lfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,47 @@

A Git extension for versioning large files (even couple GB in size).

Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.
Git Large File Storage (LFS) `replaces` large files such as audio samples, videos, datasets, and graphics with `text pointers` inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.

## Usage

First, you need to install Git LFS, then specify which files should be tracked by Git LFS. You can commit and push files to your repository as you normally would with Git. Git LFS `automatically replaces` the large files with text pointers and uploads the file contents to the Git LFS server.

When you clone a repository with LFS files or pull changes that include LFS files, Git LFS downloads the file contents `as needed`.

```sh
# Install Git LFS
git lfs install

# Specify files to be tracked
# This command creates or updates a .gitattributes file with the paths of files that should be tracked by Git LFS.
git lfs track "*.jpg"

# Commit large file
git add file.jpg
git commit -m "Add large file"
git push origin main
```

## Custom server

```sh
# Set custom lfs server url
git config lfs.url "https://your-lfs-server.com"
```

If your Git LFS server requires authentication, you'll need to provide your credentials. Git LFS uses the same credentials as Git itself, so if you're already authenticated with Git, you should be able to use Git LFS without any additional steps.

Alternatively, you can use a credential helper to store your username and password.

```sh
# Set the credential helper
git config --global credential.helper store
```

The next time you push or pull from the server, Git will ask for your `username` and `password`, and then it will store them for future use.

The `credential.helper` command sets the credential helper to `store`, which stores your credentials on disk in a plain text file. If you want a more secure method, you can use `cache` (which stores credentials in memory for a certain period of time) or `osxkeychain` (on macOS, which stores credentials in the secure keychain).

## References

Expand Down
6 changes: 2 additions & 4 deletions src/Local/git-cli/git-merge-vs-rebase.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Merge vs Rebase

> Note: This page does not cover all possible scenarios, but only commonly used strategies.
Your master/main branch should be the only source of truth for your application.
Your `main` branch should be the only source of truth for your application.

Generally, you'll create a new branch off of your main branch, add new features in your local branch, create a pull request to the main branch, and then merge your code. When you're pushing new code to you main branch, you'd want to `merge` your changes in the main branch. This is because you are creating new commits.

Every once in a while, before you merge your changes in main branch, you want your local branch to be up to date with the master/main branch. You local branch can get behind the main branch when more than one developer is working on the project. To catch up your local branch with the new commits in main branch, you'd use a strategy called `rebase`. You should never merge commits from your main branch to your local branch, because those commits were already accepted in your main branch, and your main branch is supposed to be the only source of truth.
Every once in a while, before you merge your changes in main branch, you want your local branch to be up to date with the `main` branch. You local branch can get behind the main branch when more than one developer is working on the project. To catch up your local branch with the new commits in main branch, you'd use a strategy called `rebase`. You should never merge commits from your main branch to your local branch, because those commits were already accepted in your main branch, and your main branch is supposed to be the only source of truth.

## Git Merge

Expand Down
10 changes: 5 additions & 5 deletions src/Local/git-cli/installation.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Git Installation

## Verify
```

```sh
$ git --version
```
If git is intalled locally, you'll see something like
```
git version 2.24.3 (Apple Git-128)

# If git is installed locally, you'll see something like
# git version 2.24.3 (Apple Git-128)
```

## Installation
Expand Down
2 changes: 1 addition & 1 deletion src/Local/git-cli/sub-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

It often happens that while working on one project, you need to use another project from within it. Perhaps it’s a library that a third party developed or that you’re developing separately and using in multiple parent projects. A common issue arises in these scenarios: you want to be able to treat the two projects as separate yet still be able to use one from within the other.

Git addresses this issue using submodules. Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.
Git addresses this issue using submodules. `Submodules` allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.

Submodules are registered in a `.gitmodules` files in the project repository.

Expand Down

0 comments on commit 85f96fd

Please sign in to comment.