Git is a tool used for code management. It is open source and is very helpful for code development and collaboration.

Git uses version control of code, which means every change to the code is recorded by version control in form of a database. In case of a mistake, version control allows us to go back in time, compare it to prior versions and help fix the error while causing the least amount of interruption to people who are working on that code.

Git basics to get started

Install Git

Reference: https://github.com/git-guides/install-git

Configuring SSH Keys

Reference: https://docs.github.com/en/authentication/connecting-to-github-with-ssh

Testing your Git connection

ssh -T [email protected]

Configure Git

The configured user name and email will be used to record any changes we make to the git repositories.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Clone Git repository

git clone repo-name

Git add

The git add command add new or changed files in the local working directory to the Git staging area. The staging area can be described as a preview of the next commit and holds the changes to be saved to the remote directory.

Adds a single file to the staging area

git add filename

Adds all files and changes made to the staging area

git add -A 

Git commit

Git commit is like a snapshot in time for the repository. These commits are like snapshots of our entire repository at specific times.

Make new commits often based on logical units of change. Commits contain a lot of metadata about the changes to the repository including message, author, timestamp etc. Over time, commits should tell a story of our history of our repository.

git commit -m "Change made to the code"

Git push

git push updates the remote branch with all the local commits. Pushing changes to the remote makes our commits accessible to our collaborators.

git push

Git pull

git pull gets the updates from the remote repository and applies the changes to the local copy of our repository. It is a good idea to do git pull regularly on branches that we are

git pull

Git init

git init turns any directory into a Git repository. The Git command creates a hidden directory called .git that stores all of the objects and refs To start a repository, we use either git init or git clone, but not both.

Within an already existing directory

git init

If the project does not exist yet, the following creates a new folder and initializes it as a Git repository

git init new-project-name

Examining an existing repo

Git status

git status shows the current state of the Git working directory and staging area. The command provides helpful information

  • Where HEAD is pointing to, whether that is a branch or a commit
  • Any changed files in our current directory that have not yet been committed
  • Exactly which files are the source of the conflict, in case of a merge conflict
  • If changed files are staged or not
  • If local branch is behind or ahead by any commits

By default

git status

For verbose format

git status -v

For short format

git status -s

Git log

git log allows us to see the repo’s commit history.

By default

git log

To display the last three commits

git log -n 3

The above command is not very terminal friendly and takes up a lot of space. To make it easier to view

git log --pretty=oneline

The above command while easier to view misses crucial information such as author, commit message, date etc. To show branch points and merges, and make life easier

$ git config --global alias.logline "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
$ git logline

Reference: https://ma.ttias.be/pretty-git-log-in-one-line/

To see what files were altered and how many files were altered and how many lines were added/removed for each commit

git log --stat

Git blame

git blame command is used to examine the contents of a file line by line and see when each line was last modified and who the author of the modifications was.

git blame file-name

While git log gives us the overall history of the file through the entire timeline, git blame tells us who the last one was to touch each line of code.