Git useful Commands

Basic Commands

  • git init: Initialize a new Git repository in your current directory.
  • git clone <repository>: Clone a repository from a remote source to your local machine.
  • git status: Show the current status of your working directory and staging area.
  • git add <file>: Add a file to the staging area.
  • git add .: Add all changes in the current directory to the staging area.
  • git commit -m "message": Commit changes in the staging area with a message.
  • git push <remote> <branch>: Push your committed changes to a remote repository.
  • git pull: Fetch changes from a remote repository and merge them into your current branch.

Branching and Merging

  • git branch: List all branches in your repository.
  • git branch <branch-name>: Create a new branch.
  • git checkout <branch-name>: Switch to a different branch.
  • git merge <branch-name>: Merge the specified branch into your current branch.
  • git branch -d <branch-name>: Delete a branch locally.

Viewing History and Logs

  • git log: View the commit history for the current branch.
  • git log --oneline: View a simplified version of the commit history.
  • git diff: Show changes between your working directory and the staging area.
  • git diff <branch1> <branch2>: Compare changes between two branches.

Working with Remotes

  • git remote -v: List all remote repositories associated with your local repository.
  • git remote add <name> <url>: Add a new remote repository.
  • git fetch <remote>: Fetch changes from a remote repository without merging them.
  • git push origin --delete <branch-name>: Delete a branch from the remote repository.

Stashing and Reverting

  • git stash: Temporarily save your changes without committing them.
  • git stash apply: Apply the stashed changes back to your working directory.
  • git revert <commit>: Create a new commit that undoes the changes from a specific commit.
  • git reset --hard <commit>: Reset your working directory and staging area to match a specific commit.

Tagging

  • git tag <tag-name>: Create a new tag for marking a specific commit.
  • git push origin <tag-name>: Push a tag to the remote repository.

Undoing Changes

  • git checkout -- <file>: Discard changes in a working directory file.
  • git reset HEAD <file>: Unstage a file without discarding changes.
  • git reset --soft <commit>: Reset to a previous commit but keep changes staged.
  • git reset --hard <commit>: Reset to a previous commit and discard all changes.

Migrating a Git repository from HTTPS to SSH

Step 1: Verify SSH Keys

First, ensure you have SSH keys set up on your machine and added to your GitHub account.

  1. Check for existing SSH keys:
    ls -al ~/.ssh

    Look for files named id_rsa and id_rsa.pub or similar.

  2. Generate a new SSH key (if necessary):
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    

    Follow the prompts to save the key (default location is fine).

  3. Add your SSH key to the SSH agent:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    
  4. Add the SSH key to your GitHub account: Copy the contents of your public key file to the clipboard:
    cat ~/.ssh/id_rsa.pub
    

    Then, add it to your GitHub account by going to Settings > SSH and GPG keys > New SSH key.

Step 2: Update Remote URL

  1. Navigate to your local repository:
    cd /path/to/your/repo
    
  2. Get the current remote URL:
    git remote -v
    
  3. Update the remote URL to use SSH: Replace origin with the name of your remote if it’s different.
    git remote set-url origin [email protected]:username/repo.git
    

    Replace username with your GitHub username and repo with the name of your repository.

  4. Verify the change:
    git remote -v
    

    You should see the SSH URL listed.

Step 3: Test the Connection

  1. Test the SSH connection:

    You should see a success message like “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”

  2. Fetch from the remote to ensure everything is working:
    git fetch
    

If everything is set up correctly, your repository should now be using SSH instead of HTTPS.

Configuration options for the dependabot.yml for .net Projects

# generated by dependadotnet
# https://github.com/dotnet/core/tree/main/samples/dependadotnet
version: 2
updates:
  - package-ecosystem: "nuget"
    directory: "/azure/sdk-identity-resources-storage" #AzureIdentityStorageExample.csproj
    schedule:
      interval: "weekly"
      day: "wednesday"
    open-pull-requests-limit: 5
  - package-ecosystem: "nuget"
    directory: "/csharp/expression-trees" #expression-trees.csproj
    schedule:
      interval: "weekly"
      day: "wednesday"
    open-pull-requests-limit: 5
  - package-ecosystem: "nuget"
    directory: "/core/assembly/MetadataLoadContext" #MetadataLoadContextSample.csproj
    schedule:
      interval: "weekly"
      day: "wednesday"
    open-pull-requests-limit: 5

References
https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
https://github.com/dotnet/samples/blob/main/.github/dependabot.yml

Install GitHub Desktop on Ubuntu

wget -qO - https://mirror.mwt.me/ghd/gpgkey | sudo tee /etc/apt/trusted.gpg.d/shiftkey-desktop.asc > /dev/null
# if you want to use packagecloud.io
sudo sh -c 'echo "deb [arch=amd64] https://packagecloud.io/shiftkey/desktop/any/ any main" > /etc/apt/sources.list.d/packagecloud-shiftkey-desktop.list'

# if you want to use the US mirror
sudo sh -c 'echo "deb [arch=amd64] https://mirror.mwt.me/ghd/deb/ any main" > /etc/apt/sources.list.d/packagecloud-shiftkey-desktop.list'
sudo apt update && sudo apt install github-desktop

References
https://github.com/shiftkey/desktop

Install GitHub CLI on Ubuntu

curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
sudo apt update
sudo apt install gh

References
https://github.com/cli/cli/blob/trunk/docs/install_linux.md
https://cli.github.com/manual/gh

Generate and Prepare a new GPG key for Github

Generating a GPG key

gpg --full-generate-key
gpg --list-secret-keys --keyid-format=long

From the list of GPG keys, copy the long form of the GPG key ID you’d like to use. In this example, the GPG key ID is 3AA5C34371567BD2:

$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid                          Hubot 
ssb   4096R/42B317FD4BA89E7A 2016-03-10
gpg --armor --export 3AA5C34371567BD2
# Prints the GPG key ID, in ASCII armor format

Copy your GPG key, beginning with -----BEGIN PGP PUBLIC KEY BLOCK----- and ending with -----END PGP PUBLIC KEY BLOCK-----.

References
https://docs.github.com/en/authentication/managing-commit-signature-verification/generating-a-new-gpg-key