Git Guide
This guide will walk you through the essential steps to install, configure, and use Git with Bitbucket on Manjaro Linux.
1. Installing Git on Manjaro Linux
sudo pacman -S --needed --noconfirm git && git --version
2. Configuring Git
After installing Git, you need to configure your user information. This ensures that all your commits are associated with your identity.
Set your name and email globally for all repositories:
git config --global user.name "Your Name" && git config --global user.email "youremail@example.com"
Replace "Your Name"
and "youremail@example.com"
with your actual name and email address.
You can verify the configuration with:
git config --list
3. Generating an SSH Key Pair
To securely authenticate with Bitbucket, you'll need to generate an SSH key pair. Use the following command to create an SSH key:
ssh-keygen -t ed25519 -C "youremail@example.com"
Replace "youremail@example.com"
with your actual email address. When prompted, accept the default file location by pressing Enter, and it's a good idea to set a secure passphrase.
The SSH key permissions are automatically set correctly during generation, but you can verify them:
ls -la ~/.ssh/id_ed25519
The permissions should be -rw-------
(600).
4. Starting the SSH Agent and Adding Your SSH Key
To use your SSH key, you need to start the SSH agent and add your key.
Start the SSH agent:
eval "$(ssh-agent -s)"
Add your private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519
5. Adding the SSH Key to Your Bitbucket Account
Now, you need to add the SSH public key to your Bitbucket account. First, copy the public key to your clipboard:
Option 1: Install and use xclip
:
sudo pacman -S --needed --noconfirm xclip && xclip -selection clipboard < ~/.ssh/id_ed25519.pub
Option 2: Display the key and copy manually:
cat ~/.ssh/id_ed25519.pub
Next, log into Bitbucket in your web browser and follow these steps:
- Click on your profile picture and go to Personal settings.
- Under the Security section, click SSH keys.
- Click Add key.
- In the "Label" field, provide a descriptive label for your SSH key (e.g., "Manjaro Laptop").
- Paste your public key into the "Key" field.
- Click Add key.
6. Verifying Your SSH Connection
To verify that your SSH connection is working correctly with Bitbucket, run:
ssh -T git@bitbucket.org
If everything is set up correctly, you'll see a message similar to this:
authenticated via ssh key.
You can use git to connect to Bitbucket. Shell access is disabled
This means your SSH key is correctly linked to your Bitbucket account.
7. Clone All Repositories Locally
- Create a new bash script file:
kate clone-all-repositories.sh
- Paste the following script:
#!/bin/bash
# Script to clone all accessible Bitbucket repositories using SSH.
# --- Configuration Prompts ---
read -p "Enter your Bitbucket Username (the one you log in with): " BITBUCKET_USERNAME
if [ -z "$BITBUCKET_USERNAME" ]; then
echo "Bitbucket Username is required."
exit 1
fi
read -p "Enter your Bitbucket Workspace ID (often same as username for personal repos, or your team/org name): " BITBUCKET_WORKSPACE
if [ -z "$BITBUCKET_WORKSPACE" ]; then
echo "Bitbucket Workspace ID is required."
exit 1
fi
echo "You'll need a Bitbucket App Password with 'Repositories: Read' permissions to list your repositories via the API."
echo "Create one at: Bitbucket Settings -> Personal settings -> App passwords -> Create app password."
read -sp "Enter your Bitbucket App Password: " BITBUCKET_APP_PASSWORD
echo # Newline after password input
if [ -z "$BITBUCKET_APP_PASSWORD" ]; then
echo "Bitbucket App Password is required to fetch repository lists."
exit 1
fi
# --- Dependency Checks ---
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. jq is required to parse API responses."
echo "Installing jq..."
sudo pacman -S --noconfirm jq
fi
if ! command -v curl &> /dev/null; then
echo "Error: curl is not installed. curl is required to make API requests."
echo "Installing curl..."
sudo pacman -S --noconfirm curl
fi
echo "Fetching repository list from Bitbucket for workspace '$BITBUCKET_WORKSPACE'..."
# --- API Call to Get SSH Clone URLs (Handles Pagination) ---
API_BASE_URL="https://api.bitbucket.org/2.0/repositories/${BITBUCKET_WORKSPACE}?pagelen=50&role=member"
CURRENT_PAGE_URL="$API_BASE_URL"
ALL_SSH_URLS=""
while [ -n "$CURRENT_PAGE_URL" ]; do
echo "Fetching page: $CURRENT_PAGE_URL"
API_RESPONSE=$(curl -s -u "${BITBUCKET_USERNAME}:${BITBUCKET_APP_PASSWORD}" "$CURRENT_PAGE_URL")
# Basic check for API error response
if echo "$API_RESPONSE" | jq -e '.type == "error"' > /dev/null 2>&1; then
echo "Error fetching repositories from Bitbucket:"
echo "$API_RESPONSE" | jq '.error.message'
CURRENT_PAGE_URL=""
if [ -z "$ALL_SSH_URLS" ]; then
echo "Failed to fetch any repository data. Check credentials and workspace ID."
exit 1
fi
echo "Continuing with any repositories fetched so far..."
break
fi
PAGE_SSH_URLS=$(echo "$API_RESPONSE" | jq -r '.values[]?.links.clone[]? | select(.name=="ssh").href')
if [ -n "$PAGE_SSH_URLS" ]; then
ALL_SSH_URLS="${ALL_SSH_URLS}${PAGE_SSH_URLS}"$'\n'
fi
# Get the next page URL from the API response
CURRENT_PAGE_URL=$(echo "$API_RESPONSE" | jq -r '.next // empty')
done
# Remove any trailing newline from ALL_SSH_URLS if it's not empty
if [ -n "$ALL_SSH_URLS" ]; then
ALL_SSH_URLS=$(echo "$ALL_SSH_URLS" | sed '/^$/d')
fi
if [ -z "$ALL_SSH_URLS" ]; then
echo "No repositories found with SSH clone links. Check your workspace, permissions, and app password."
exit 1
fi
echo "Found repositories. Starting cloning process..."
echo "Repositories will be cloned into subdirectories named after each repository."
# --- Loop and Clone ---
SUCCESS_COUNT=0
FAIL_COUNT=0
SKIPPED_COUNT=0
TOTAL_REPOS=$(echo "$ALL_SSH_URLS" | wc -l)
CURRENT_REPO_NUM=0
while IFS= read -r SSH_URL; do
if [ -z "$SSH_URL" ]; then
continue
fi
((CURRENT_REPO_NUM++))
REPO_NAME=$(basename "$SSH_URL" .git)
echo
echo "--- [${CURRENT_REPO_NUM}/${TOTAL_REPOS}] Processing repository: $REPO_NAME ---"
if [ -d "$REPO_NAME" ]; then
echo "Directory '$REPO_NAME' already exists. Skipping clone."
echo "To update, manually run: cd \"$REPO_NAME\" && git pull"
((SKIPPED_COUNT++))
else
echo "Cloning $REPO_NAME..."
if git clone "$SSH_URL"; then
echo "Successfully cloned $REPO_NAME."
((SUCCESS_COUNT++))
else
echo "ERROR: Failed to clone $REPO_NAME. Check SSH key setup and repository access."
((FAIL_COUNT++))
fi
fi
done <<< "$ALL_SSH_URLS"
echo
echo "--- Cloning Summary ---"
echo "Successfully cloned: $SUCCESS_COUNT"
echo "Skipped (already exists): $SKIPPED_COUNT"
echo "Failed to clone: $FAIL_COUNT"
echo "-----------------------"
echo "Cloning process finished."
if [ "$FAIL_COUNT" -gt 0 ]; then
echo "Please review the output for any failed clones and troubleshoot accordingly."
fi
- Make the script executable:
chmod +x clone-all-repositories.sh
- Run the script:
./clone-all-repositories.sh
- Use any git GUI of your choice to manage your cloned repositories.
8. Cloning a Repository
You can now clone a repository from Bitbucket using SSH. Use the following command to clone a specific repository:
git clone git@bitbucket.org:yourusername/yourrepository.git
Replace "yourusername"
with your Bitbucket username and "yourrepository"
with the repository name.
9. Making Changes and Committing Them
After making changes in your cloned repository, follow these steps to commit your changes:
- Stage your changes with
git add
. This tells Git which files you've changed and want to commit:
git add <filename>
To stage all changes at once, use:
git add .
- Commit your changes with a message that describes what you’ve done:
git commit -m "Descriptive commit message"
Make sure to write a clear and concise commit message to describe your changes.
10. Pushing Your Changes
To push your changes to Bitbucket, use the following command:
git push origin main
If you're working on a branch other than main
, replace "main"
with the branch name you're pushing to:
git push origin <branch-name>
11. Creating and Switching Branches (Optional)
If you want to create a new branch (e.g., for a new feature or bug fix), you can do so with:
git checkout -b new-branch
This will create and switch to a new branch called "new-branch"
.
After making changes on the new branch, you can push it to Bitbucket like this:
git push origin new-branch
12. Checking Repository Status and History
Here are some helpful commands to check the status and history of your repository:
Check the current status of your repository (files modified, staged, etc.):
git status
View the commit history:
git log
13. Pulling Updates from Bitbucket
To keep your local repository up to date with the latest changes from Bitbucket, use:
git pull origin main
If you're working on a different branch, replace "main"
with the appropriate branch name:
git pull origin <branch-name>