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.

GitHub CLI

  1. Check GitHub authentication status:
gh auth status

#return
github.com
  ✓ Logged in to github.com account <username> (keyring)
  - Active account: true
  - Git operations protocol: https
  - Token: gho_************************************
  - Token scopes: 'gist', 'read:org', 'repo', 'workflow'

  ✓ Logged in to github.com account <username> (keyring)
  - Active account: false
  - Git operations protocol: https
  - Token: gho_************************************
  - Token scopes: 'gist', 'read:org', 'repo', 'workflow'
  1. Login a GitHub account:
gh auth login --hostname github.com --git-protocol https --web
  1. Switch between GitHub accounts:
gh auth switch --hostname github.com --user <username>
  1. Create a repo:
# inside the repo
git init                                                                                                                                                                                                                                                                                                 
git add .
git commit -m "initial commit"                                                                                                                                                                                                                                                                           
                  
# create the remote on GitHub and link it as 'origin'                                                                                                                                                                                                                                                    
gh repo create <username>/<repo_name> --private --source=. --remote=origin --push

If the repo already exists on GitHub and you just want to push:

git remote add origin https://github.com/<username>/<repo_name>.git
git push -u origin main

Multiple SSH Keys

If you already have an SSH key bound to one GitHub account and need to work with a second GitHub account on the same machine, generate an additional SSH key with a distinct suffix and configure SSH to use the right key per account.

1. Create a New SSH Key with a Suffix

Generate a second key file alongside your existing ~/.ssh/id_ed25519. Use the -f flag to give it a distinct filename (suffix) so it does not overwrite the original key.

$ ssh-keygen -t ed25519 -C "your_work_email@example.com" -f ~/.ssh/id_ed25519_work

This produces two new files:

  • ~/.ssh/id_ed25519_work — the private key
  • ~/.ssh/id_ed25519_work.pub — the public key

You can pick any suffix that helps you remember which account the key belongs to (e.g. _work, _personal, _clientname).

2. Add the New Key to the ssh-agent

Start the ssh-agent (if it is not running) and add the new private key.

$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_ed25519_work

On macOS, store the passphrase in the keychain so you do not need to re-enter it:

$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work

3. Add the Public Key to the Second GitHub Account

Copy the contents of the new public key to your clipboard.

# macOS
$ pbcopy < ~/.ssh/id_ed25519_work.pub

# Linux (requires xclip)
$ xclip -selection clipboard < ~/.ssh/id_ed25519_work.pub

# Windows (Git Bash)
$ clip < ~/.ssh/id_ed25519_work.pub

Then:

  1. Sign in to the second GitHub account.
  2. Go to Settings → SSH and GPG keys → New SSH key.
  3. Give it a descriptive title (e.g. Work Laptop — work account).
  4. Paste the public key and click Add SSH key.

4. Configure ~/.ssh/config with a Host Alias

Edit (or create) ~/.ssh/config and define a host alias for the second account. The alias (github.com-work below) is what you will use in place of github.com in remote URLs.

# Default account (existing key)
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

# Second account (new key)
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes

The IdentitiesOnly yes setting forces SSH to use only the specified key, preventing the agent from offering the wrong key first.

Test both connections:

$ ssh -T git@github.com
Hi <personal-username>! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@github.com-work
Hi <work-username>! You've successfully authenticated, but GitHub does not provide shell access.

5. Use the Alias in Repository Remotes

When cloning or setting the remote for a repository that belongs to the second account, replace github.com with your alias github.com-work.

Clone a new repository:

$ git clone git@github.com-work:work-org/repo.git

Update an existing repository to use the alias:

$ git remote set-url origin git@github.com-work:work-org/repo.git
$ git remote -v
origin  git@github.com-work:work-org/repo.git (fetch)
origin  git@github.com-work:work-org/repo.git (push)

Repositories belonging to your original account continue to use the normal git@github.com:... URL and the original key — no changes needed.

6. Set the Correct Commit Identity Per Repository

Your global user.name / user.email should match your default account. For each repository belonging to the second account, override the identity locally so commits are attributed correctly.

$ cd ~/path/to/work-repo
$ git config --local user.name "Your Work Name"
$ git config --local user.email "your_work_email@example.com"

Verify the configuration:

$ git config --local --get user.email
your_work_email@example.com

7. (Optional) Auto-Apply Identity by Directory

If you keep all work repositories under a single directory (e.g. ~/work), you can have Git automatically apply the right identity using includeIf in your global config.

Create ~/.gitconfig-work:

[user]
  name = Your Work Name
  email = your_work_email@example.com

Then add to ~/.gitconfig:

[user]
  name = Your Personal Name
  email = your_personal_email@example.com

[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work

Now any repository under ~/work/ automatically uses your work identity, and everything else uses your personal identity.

Previous
Docker