Elena' s AI Blog

GIT in 10 minutes

04 Dec 2021 (updated: 03 Jul 2026) / 14 minutes to read

Elena Daehnhardt


GIT in 10 minutes


TL;DR:
  • A beginner's guide to Git version control: setting up Git repositories, using command-line and GUI tools, and managing code versions with distributed repository management.

Previous: Part 32 — Live Design Testing with GitHub Pages and a Custom Domain

Next: Part 1 — Git Commands and a Contribution Workflow

What Is Git? Distributed Version Control Overview

Git is a distributed version control system that tracks changes to files and coordinates work on those files among multiple people. Version control systems are handy to keep track of file versions, which is useful for tracking your code, scripts, and text information. Currently, Git is one of the best open-source and cross-platform version control solutions. Git enables distributed repository management and works fast over HTTP and SSH protocols. Git is relatively easy to use, with a command-line utility or a Graphical User Interface.

Version Control with Git: Distributed Repository Management

Version control systems are handy to keep track of file versions, which is useful for tracking your code, scripts, and text information. Currently, Git is one of the best open-source and cross-platform version control solutions. Git enables distributed repository management and works fast over HTTP and SSH protocols. Git is relatively easy to use, with a command-line utility or a Graphical User Interface.

Personally, I have found several commands to be essential for tracking my thesis text sources (latex) and versions of code. In this brief tutorial I will share these commands with you. For simplicity reasons, I will give a starting point of setup, initialising a GIT repository, basic usage scenarios in just 10 minutes of your time.

Installing Git on macOS with Homebrew

Since I work on macOS, I use the Homebrew package manager to install Git. To install Homebrew, we can get it from https://brew.sh or with:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Next, Git installation with Homebrew, and Git’s location and version:

# Installing git with brew
brew install git
# Checking its path
which git
# Checking its version
git --version

Git Setup: Working Between Local and Remote Repositories

I keep my file copies locally on a few devices and remotely at GitHub and BitBucket websites. GIT allows me to pull and push my code or text between local and remote copies. Next, I will explain how I use GIT via the command line (in Terminal) on MAC OS. I will precede every command with a comment to explain what exactly it does.

Creating a Remote Git Repository on GitHub or Bitbucket

Firstly, we can go to Bitbucket or GitHub website, create our user account. It would make sense to create the first repository. To avoid confusion, I prefer to name my local and remote repositories with the same name.

Initializing a Local Git Repository with git init

Locally, I went to the ~/Documents/elena directory, created there folder called /tutorials and went into it:

cd /Users/my_user_name/Documents/elena
mkdir tutorials
cd tutorials

With “git init .” we initialise the GIT repository inside of the /tutorials folder:

git init .

Creating the .gitignore file to define which files should not be tracked.DS_Store and the compiled Python byte code files:

touch .gitignore
nano .gitignore

Write these two lines (you can add more file templates that you do not want to track) into .gitignore file:

.DS_Store
*.pyc

Configuring Git User Name and Email with git config

You can see your personal e-mail address used for your commits in Git by default:

git config --global user.email

To override the default global settings for the user name and email, we can configure an individual repository with a different identity. For this, we run in the root folder of the repository the following commands:

git config user.name "My name"
git config user.email my@email.com

To change the default user identity, run the following (stored in ~/.gitconfig):

git config --global user.name "Your Name Here"
git config --global user.email your@email.com

Adding and Committing Files with git add and git commit

Adding all files (except) of the defined in the .gitignore to the current repository:

git add .

Committing these files with the text string indicating the “Initial commit”:

git commit -m "Initial commit"

Pushing and Pulling with Git Remote Repositories

Next, we define the remote repository location, which can be seen from the website of remote repository host (BitBucket, GitHub, or even on your own web server with GIT server installed):

git remote add origin git@bitbucket.org:my_user_name/tutorials.git

Pushing the locally added files into the remote location called “origin” with the master branch (this we will explain in the “Advanced” section if you are interested):

git push origin master

Congratulations! We are successfully uploaded files into the remote GIT server! Please let me know if this did not work out for you.

When you change your local files, we can commit them as follows (you can change your commit message “minor changes” as you please ):

git commit -a -m "minor changes"

In case when we update the remote repository and want to pull the changes at another device, we can use:

git pull origin master

Essential Git Workflow Commands Reference

git init --bare --share 				# Creating a bare repository inside of the current folder
git clone git@bitbucket.org:my_user_name/tutorials.git 	# Cloning the remote repository for working with it locally

git log							# Log commits
git status 						# Check the status 
git status -u						# To see Untracked Files

git diff commit1 commit2				# See differences between two commits
git diff --name-only commit1 commit2			# See the files changed between commits

mkdir tools						# creating /tools directory for further 
git mv git.md tools					# moving the git.md file into it
git status						# see the status of operation
git commit -a -m "Moved git.md into /tools"		# committing the move

git add -all /code    				        # Add all untracked files 
git commit -a -m "code directory added"  		# Update changes

git diff						# See changes, unstaged (not committed yet)
git diff --staged					# See staged changes

git fetch origin  					# Updating my local repository with new info on Remote 	

git checkout -- file				        # Revert file to the latest commit

git remote -v						# See remotes
git remote show origin					# Inspecting remote
git diff origin/master					# See differences between local changes and remote
git remote rename myremoterepository origin		# rename remote myremoterepository to new name origin

git push origin						# Pushes local changes to origin
git pull origin master				        # Pulls from remote and merges    

git show --pretty="format:" --name-only commit_id  	# show files' changes in commit_id

Git Authentication with SSH Keys (ssh-keygen)

Suppose you want to use a secure connection and private GIT repositories. In that case, it makes sense to set up the SSH private key on your local machine and configure the Bitbucket settings with the public key. The use of public and private keys will ensure a secure encrypted connection and facilitate the user login without re-entering the user password. Use ssh-keygen (MAC OS) or PuTTYgen (for Windows) for generating the SSH keys as explained in the sources below.

Git Authentication with GitHub Personal Access Tokens

Last but not least, in GitHub, we can use personal access tokens to authenticate from the command line. We can perform git commands via HTTPS protocol [GIT user-manual last updated in 2.16.1]. For instance, use the following commands for creating the remote connection with a web address and further cloning the repository:

git remote set-url origin https://<token>@github.com/<my_user_name>/<repository_name>
git clone https://<token>@github.com/<my_user_name>/<repository_name>

Common Git Errors and Fixes

Two errors are common when you run the setup steps above for the first time: a rejected first push and an SSH authentication failure.

Fixing “Updates were rejected” / “src refspec master does not match” on git push

When git push origin master fails with error: src refspec master does not match any or Updates were rejected because the remote contains work that you do not have locally, the cause is one of two things: you have not created a commit yet, or the remote already has commits your local branch lacks.

  • No commit yet: run git add . then git commit -m "Initial commit" before pushing. Git cannot push a branch that has no commits.
  • Branch name mismatch: newer Git defaults to main, not master. Check with git branch; push the branch you actually have, e.g. git push origin main.
  • Remote is ahead: integrate the remote history first, then push:
git pull origin main --rebase
git push origin main

For a deeper walkthrough of the non-fast-forward case, see Fix non-fast-forward push errors.

Resolving “Permission denied (publickey)” on git push or git clone

The error git@github.com: Permission denied (publickey). fatal: Could not read from remote repository means Git connected over SSH but the server did not accept your key. This happens when the SSH key from the setup section was never generated, never added to the agent, or never uploaded to GitHub/Bitbucket.

# 1. Verify a key exists (look for id_ed25519.pub or id_rsa.pub)
ls -al ~/.ssh
# 2. Generate one if missing
ssh-keygen -t ed25519 -C "your@email.com"
# 3. Add it to the SSH agent
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519
# 4. Copy the PUBLIC key and paste it into GitHub/Bitbucket SSH settings
cat ~/.ssh/id_ed25519.pub
# 5. Test the connection
ssh -T git@github.com

If you would rather authenticate over HTTPS instead of SSH, use a personal access token as described in the Git Authentication with GitHub Personal Access Tokens section above.

References and Further Reading

Thanks to the authors of the following websites that helped me to learn:

GIT user-manual last updated in 2.16.1

The most useful git commands

Set up an SSH key

SSH login without password in OS X and Linux

Creating a personal access token

Git Setup and Workflow Summary

Git is a distributed version control system that, once configured with a remote on GitHub or Bitbucket, lets you commit, push, pull, and track every change to your codebase. We created a Git repository, set up its connection with Github, committed and pushed changes, and learned how to get repository status and log information. In the next post, I will go into the collaborative usage of GIT with multiple users.

Did you like this post? Please let me know if you have any comments or suggestions.

Git posts that might be interesting for you



Git FAQ

What is the difference between git fetch and git pull?

git fetch downloads commits and refs from the remote into your local repository but does not change your working files. git pull runs git fetch and then immediately merges (or rebases) the fetched changes into your current branch. Use git fetch when you want to inspect remote changes before integrating them.

Should I use SSH or HTTPS to connect to GitHub?

Use SSH when you push frequently and want passwordless authentication after a one-time key setup. Use HTTPS with a personal access token when you work behind a restrictive firewall or cannot configure SSH keys. Both are equally secure; SSH is more convenient for daily use.

What does git init actually do?

git init creates a hidden .git directory in the current folder that stores the entire version history and configuration. The command turns an ordinary directory into a Git repository; it does not upload anything or contact a remote server.

Why should I add a .gitignore file?

A .gitignore file tells Git which files to leave untracked, such as .DS_Store, compiled *.pyc byte code, virtual environments, and secret keys. Adding it before your first commit keeps generated and sensitive files out of version control.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2021) 'GIT in 10 minutes', daehnhardt.com, 04 December 2021. Available at: https://daehnhardt.com/blog/2021/12/04/edaehn-git/
All Posts