UP | HOME
Ravi Sagar | Home | Blog

Mastering Git

Table of Contents

1 Introduction

Git is one of the most important and key skill that we have to acquire especially for software development. I am an Atlassian Consultant which is my main job. I help companies and teams get the most out Atlassian tools like Jira, Confluence, Bitbucket and help them deliver fast and efficiently. I often work with development teams and they use Git as their version control system.

2 Git config

Get configured user's email and name.

git config --get user.name
git config --get user.email

3 Branching

Create a branch and switch to it.

git checkout -b <branch-name>

Create a branch without switching and switch to it later.

git branch <branch-name>
git checkout <branch-name>

Go back to your branch.

git branch main

Check your branches.

git branch

Fetch all branches.

git fetch --all

Create branch from another branch.

git checkout -b SOTF-1_small-changes-before-go-live

4 Delete commit from Bitbucket

Let us say you have committed some sensitive information to a repository and if you want to delete it then yes it is possible.

First find the commit numbers and pick a one which is may be one of your first commit.

git reset --hard 2342342
git push -f

The force commit at the end will bring your repository back to that commit and it will also delete the commits.

5 DONE Setup multiple keys on Bitbucket

Steps here: https://support.atlassian.com/bitbucket-cloud/docs/managing-multiple-bitbucket-user-ssh-keys-on-one-device/ Also check what I configured on local.

First generate the ssh key pair.

ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f ~/.ssh/id_rsa_username

Then modify the ~/.ssh/config.

Host bitbucket.org-id_rsa
     User git
     HostName bitbucket.org
     PreferredAuthentications publickey
     IdentitiesOnly yes
     IdentityFile ~/.ssh/id_rsa

 Host bitbucket.org-id_rsa_username
     User git
     HostName bitbucket.org
     PreferredAuthentications publickey
     IdentitiesOnly yes
     IdentityFile ~/.ssh/id_rsa_username

Host bitbucket.org-id_rsa_username2
     User git
     HostName bitbucket.org
     PreferredAuthentications publickey
     IdentitiesOnly yes
     IdentityFile ~/.ssh/id_rsa_username2

Finally modify the existing git repositories remote.

git remote set-url origin git@bitbucket.org-id_rsa_username:{workspace}/{repo}.git

For a new repo do a clone.

git clone git@bitbucket.org-id_rsa_username:{workspace}/{repo}.git

So basically the ~/.ssh/config file and the git repository remote url will ensure that the right ssh-key is used.

Video: https://youtu.be/U5ddj4SytPY

6 Remove file from git but not locally

git rm -r --cached path/to/file

7 Errors

7.1 Can't git push to Bitbucket: Unauthorized - fatal: Could not read from remote repository

Remove the SSH key from the repo. (Click on repo name > Settings > Access Keys) Add SSH key to Account settings SSH keys. (Click on your avatar > Personal Settings > SSH Keys)

Change .git/config to use ssh instead of https. Replace this url.

url = https://sparxsys@bitbucket.org/sparxsys-team/ravisagar_new_site.git

with this url

url = git@bitbucket.org:sparxsys-team/ravisagar_new_site.git