Git + Bitbucket

Bitbucket

Wichtig:

NICHT:

git clone https://fenomedia@bitbucket.org/fenomedia/homepage-3d.gitCode-Sprache: PHP (php)

SONDERN:

git clone fenomedia@bitbucket.org:fenomedia/homepage-3d.git Code-Sprache: PHP (php)

SSH Authentifizierung

Install OpenSSH on macOS

A version of OpenSSH should be pre-installed on macOS. To check if OpenSSH is installed, open a terminal and run:

<code>ssh -V</code>Code-Sprache: HTML, XML (xml)

The output should show the installed version of OpenSSH.

To use Homebrew to install a newer version of OpenSSH, run:

<code>brew install openssh</code>Code-Sprache: HTML, XML (xml)

To check that OpenSSH was installed successfully, run:

<code>ssh -V</code>Code-Sprache: HTML, XML (xml)

The output should show the installed version of OpenSSH.

Start the SSH agent

To allow git to use your SSH key, an SSH agent needs to be running on your device.

To check if it is already running, run the ps command. If the ssh-agent is already running, it should appear in the output, such as:

<code>$ ps -a | grep ssh-agent </code>
<code>myusername 3291 0.0 0.0 6028 464 ? Ss 07:29 0:00 ssh-agent</code>Code-Sprache: HTML, XML (xml)

To start the agent, run:

<code>eval $(ssh-agent)</code>Code-Sprache: HTML, XML (xml)

You may need to add this command to your ~/.bashrc, ~/.zshrc, ~/.profile, or equivalent shell configuration file. Adding this command to a shell configuration file will ensure the agent is running when you open a terminal.

Create an SSH key pair

To create an SSH key pair:

  • Open a terminal and navigate to your home or user directory using cd, for example:
<code>cd ~</code>Code-Sprache: HTML, XML (xml)
  • Generate a SSH key pair using ssh-keygen, such as:
ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f {ssh-key-name}Code-Sprache: JavaScript (javascript)
  • Where:
    • {username@emaildomain.com} is the email address associated with the Bitbucket Cloud account, such as your work email account.
    • {ssh-key-name} is the output filename for the keys. We recommend using a identifiable name such as bitbucket_work.
  • When prompted to Enter passphrase, you can either provide a password or leave the password empty. If you input a password, you will be prompted for this password each time SSH is used, such as using Git command that contact Bitbucket Cloud (such as git push, git pull, and git fetch). Providing a password will prevent other users with access to the device from using your keys.

Once complete, ssh-keygen will output two files:

  • {ssh-key-name} — the private key.
  • {ssh-key-name}.pub — the public key.

Add your key to the SSH agent

To add the SSH key to your SSH agent (ssh-agent):

  • Run the following command, replacing the {ssh-key-name} with the name of the private key:
ssh-add ~/{ssh-key-name}
  • To ensure the correct SSH key is used when connecting to Bitbucket, update or create your SSH configuration file (~/.ssh/config) with the following settings:
Host bitbucket.org 
 AddKeysToAgent yes 
 IdentityFile ~/.ssh/{ssh-key-name}Code-Sprache: JavaScript (javascript)
  • Where {ssh-key-name} is the location of the private key file once it has been added to the ssh-agent.

Provide Bitbucket Cloud with your public key

To add an SSH key to a Bitbucket repository:

  1. At bitbucket.org, navigate to the repository and select Repository settings.
  2. Under Security, select Access keys.
  3. Select Add key.
  4. In the Add SSH key dialog, provide a Label to help you identify which key you are adding. For example, Work Laptop <Manufacturer> <Model>. A meaning full label will help you identify old or unwanted keys in the future.
  5. Open the public SSH key file (public keys have the .pub file extension) in a text editor. The public key should be in the .ssh/ directory of your user (or home) directory. The contents will be similar to:
ssh-ed25529 LLoWYaPswHzVqQ7L7B07LzIJbntgmHqrE40t17nGXL71QX9IoFGKYoF5pJKUMvR+DZotTm user@example.comCode-Sprache: CSS (css)
  1. Copy the contents of the public key file and paste the key into the Key field of the Add SSH key dialog.
  2. Select Add SSH key.
    • If the key is added successfully, the dialog will close and the key will be listed on the Access keys page.
    • If you receive the error That SSH key is invalid, check that you copied the entire contents of the public key (.pub file).

Check that your SSH authentication works

To test that the SSH key was added successfully, open a terminal on your device and run the following command:

<code>ssh -T git@bitbucket.org</code>Code-Sprache: HTML, XML (xml)

If SSH can successfully connect with Bitbucket using your SSH keys, the command will produce output similar to:

<code>authenticated via ssh key. </code>Code-Sprache: HTML, XML (xml)

Github + SSH Key Authentifizierung

SSH keypair setup for GitHub (or GitHub/GitLab/BitBucket, etc, etc)

Create a repo.

Make sure there is at least one file in it (even just the README.md)

Generate a SSH key pair (private/public):

ssh-keygen -t rsa -C "your_email@example.com"
Code-Sprache: JavaScript (javascript)

or even better:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Code-Sprache: JavaScript (javascript)

It also possible to use ed25519. There are pros and cons, but personally I’ve had some issues and that is the reason I’ve chosen to stick to 4096 rsa for now.

Copy the contents of the public SSH key

macOS:

pbcopy &lt; ~/.ssh/id_rsa.pub
Code-Sprache: HTML, XML (xml)

GNU/Linux (requires the xclip package):

xclip -sel clip &lt; ~/.ssh/id_rsa.pub
Code-Sprache: HTML, XML (xml)

Windows Command Line:

type %userprofile%\.ssh\id_rsa.pub | clip

Git Bash on Windows / Windows PowerShell:

cat ~/.ssh/id_rsa.pub | clip
Code-Sprache: JavaScript (javascript)

or ofcourse copy it via your favorite editor, cat, or whatever suits your needs 🙂

Copy the public SSH key to GitHub

Copy the contents of the to your SSH keys to your GitHub account settings (https://github.com/settings/keys).

Test the SSH key:

ssh -T git@github.com
Code-Sprache: CSS (css)

Change directory into the local clone of your repository (if you’re not already there) and run:

git remote set-url origin git@github.com:username/your-repository.git
Code-Sprache: JavaScript (javascript)

Now try editing a file (try the README) and then do:

git add -A
git commit -am &quot;Update README.md&quot;
git push
Code-Sprache: HTML, XML (xml)

Add the key to the ssh-agent

ssh-add ~/.ssh/id_rsa
Code-Sprache: JavaScript (javascript)

You should not be asked for a username or password. If it works, your SSH key is correctly configured.

Git – Verschlüsselung RSA auf MacOS Ventura

Die Standardverschlüsselung unter MacOS Ventura hat sich geändert. Dementsprechend muss man, wenn man einen RSA-verschlüsselten Key benutzen möchte, die Verschlüsselung in der SSH-Config angeben.

Dazu trägt man in der ~/.ssh/config folgende Zeilen zusätzlich ein:

Host *
    HostkeyAlgorithms +ssh-rsa
    PubkeyAcceptedAlgorithms +ssh-rsa

Git – CheatSheet

Remote-URL ändern

Bash
git remote set-url origin https://gitlab.com/KodeKloud/repository-1.git
Bash

Stashing (Zwischenspeichern)

Bash
git stash // Git Stand zwischenspeichern
git stash pop // Zu letztem Git Stand zurückkehren und diesen gleich löschen
git stash apply [stash-name] // Zu entsprechenden Git-Stand wechseln
git stash drop [stash-name] // Entsprechenden Git-Speicherstand löschen
git stash save "Name des Stashs" // Stash mit Namen speichern
git stash list // Alles stashes auflisten
Bash

Artikel

Branching

Bash
git branch // Alle Branches anzeigen
git checkout -b [BRANCHNAME] // Neuen Branch anlegen und gleich in diesen wechseln
git branch -d [BRANCHNAME] // Löschen des lokalen Branches
git push origin --delete [BRANCHNAME] // Löschen des Remotebranches
Bash

Artikel | Artikel zum Branchlöschen

Merging

Bash
git checkout master // HEAD / Master auschecken
git merge [BRANCHNAME] // Änderungen aus [BRANCHNAME] in master zusammenführen
Bash

Artikel

Diff

Bash
git diff --name-only master...xyz // Alle geänderten Dateien in einem Branch auflisten
git diff-tree --no-commit-id --name-only -r bd61ad98 // Alle geänderten Dateien in einem Commit auflisten
Bash

Geänderte Dateien auflisten

Verzeichnis aus git NICHT lokal löschen

Bash
git rm -r --cached myFolder
Bash

Gelöschte Datei aus altem Stand ansehen

Bash
git log -- path/to/file
git show <commit_hash>:path/to/file
# BSP:
git show 3a9fbc1:src/config.php
Bash

Gelöschte Datei aus altem Stand zum Vergleich wiederherstellen

Bash
git checkout <commit_hash>^ -- path/to/file
# BSP:
git checkout 3a9fbc1^ -- src/config.php
Bash

Git Merge Branches

git checkout better_branch
git merge --strategy=ours master # keep the content of this branch, but record a merge
git checkout master
git merge better_branch # fast-forward master up to the merge

Um die Historie besser nachvollziehen zu können:

git merge --strategy=ours --no-commit master
git commit # add information to the template merge message

Quelle: Stackoverflow

Windows // Globaler Git-SSH Key

If you use „Git for Windows“
>cd c:\Program Files\Git\etc\ssh\

add to ssh_config following:

AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
IdentityFile ~/.ssh/id_rsa_test

ps. you need ssh version >= 7.2 (date of release 2016-02-28)

WICHTIG: Über Putty-Keygen muss der Private-Key über Conversions > Export OpenSSH Key (force new file format) exportiert werden