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 Remove file from git but not locally

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

6 Errors

6.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