Development

Git

What is Git

Sit commodi iste iure molestias qui amet voluptatem sed quaerat. Nostrum aut pariatur. Sint ipsa praesentium dolor error cumque velit tenetur.

GUI Clients

GitHub Desktop


SSH

Windows

Open terminal and run the following command.

> ssh-keygen -t ed25519 -C "your_email@example.com"

If you are using a legacy system that doesn't support the Ed25519 algorithm, use:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

macOS

Open terminal and run the following command.

> ssh-keygen -t ed25519 -C "your_email@example.com"

Linux

Open terminal and run the following command.

> ssh-keygen -t ed25519 -C "your_email@example.com"

Git Commands

Initialize a Repository

Create a new Git repository in your project directory.

> git init

This command creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton.

Configure User Information

Set your username and email address which will be associated with your commits.

> git config --local user.name "Your Name"
> git config --local user.email "your_email@example.com"

To set these globally for all repositories on your computer:

> git config --global user.name "Your Name"
> git config --global user.email "your_email@example.com"

Add Files to the Repository

Start tracking files in your repository.

> git add <file>

# Add all files in the current directory
> git add .

Make Your First Commit

Commit your changes to the repository.

> git commit -m "Initial commit"

Connect to a Remote Repository

Link your local repository to a remote repository.

> git remote add origin https://github.com/username/repository.git

Push to Remote Repository

Push your commits to the remote repository.

> git push -u origin main

Note: If you're using an older version of Git, your default branch might be named master instead of main.

Remote

List all remotes for the current repository.

> git remote -v

Replace the URL of the remote repository.

> git remote set-url origin https://github.com/username/repository.git
> git push -u origin main

Or you can add a new remote.

> git remote add github https://github.com/username/repository.git
> git push -u github main

Remove a remote.

> git remote remove <remote-name>

# example:
> git remote remove github

Discard Changes

Discard Changes in Working Directory

To discard changes in a specific file that hasn't been staged yet:

> git checkout -- <file>

# Example:
> git checkout -- index.html

Using the newer Git syntax (Git 2.23+):

> git restore <file>

# Example:
> git restore index.html

Discard Staged Changes

To unstage changes that have been added to the staging area:

> git reset HEAD <file>

# Example:
> git reset HEAD index.html

Using the newer Git syntax (Git 2.23+):

> git restore --staged <file>

# Example:
> git restore --staged index.html

Discard All Local Changes

To discard all local changes in your working directory:

> git reset --hard

This will reset all tracked files to match the most recent commit.

Discard All Changes Including Untracked Files

To remove all changes including untracked files and directories:

> git clean -fd

The -f flag forces the clean and -d includes directories. Use with caution as this permanently deletes files.

Revert to a Specific Commit

To discard all changes and revert to a specific commit:

> git reset --hard <commit-hash>

# Example:
> git reset --hard a1b2c3d

Warning: The --hard flag will discard all changes. Make sure you have committed or backed up any important changes before using these commands.

Pull Latest Changes

Basic Pull

To fetch and merge changes from the remote repository to your current branch:

> git pull

This is equivalent to running git fetch followed by git merge.

Pull from Specific Remote and Branch

To pull from a specific remote and branch:

> git pull <remote> <branch>

# Example:
> git pull origin main

Pull with Rebase

To pull changes and rebase your local commits on top of the remote changes instead of creating a merge commit:

> git pull --rebase

# Example with specific remote and branch:
> git pull --rebase origin main

This creates a cleaner project history by avoiding unnecessary merge commits.

Fetch Only (Without Merging)

If you want to download all changes from the remote but not automatically merge them:

> git fetch

# Fetch from specific remote:
> git fetch origin

After fetching, you can inspect the changes before merging:

> git diff origin/main

Update All Tracking Branches

To fetch all remotes and update all tracking branches:

> git fetch --all

Pull with Specific Options

To pull while handling conflicts in a specific way:

# Always create a merge commit:
> git pull --no-rebase

# Only pull if it can be fast-forwarded (no divergent changes):
> git pull --ff-only

Tip: If you have uncommitted changes, Git will try to merge the pulled changes without affecting your local modifications. However, it's generally good practice to commit or stash your changes before pulling.

Previous
Docker