Git is a distributed version control system that allows developers to track changes in their code using a series of snapshots, stored in a database called the repository.

Key features of Git include:

In order to better understand GIT, I strongly recommend the use of GIT command line interface.

Key steps to not forget after installing and configuring GIT:

Creating commits(snapshots)

#1 Initializing a repository

git init - initializes or re-initializes a new Git repository, which includes creating a .git directory containing subdirectories for objects, refs/heads, refs/tags, and template files. It also establishes an initial branch that has no commits yet.

#2 Basic GIT workflow

The fundamental GIT workflow is structured around three key components:

When you use git add you are moving files from the working directory to the staging area. Whereas you use git commit you are moving files from the staging area to the repository.

#3 Writing a commit message

When I write a commit message, I follow these guidelines:

#4 Common GIT commands

Remove files

To remove a file from git you need to remove it from the working directory and add the change to the staging area.

Or you can use the shortcut git rm command to remove the file from both the working directory and the staging area:

git rm <filename>

Rename files

To rename a file in git you need to rename it in the working directory and add the change to the staging area.

Or you can use the shortcut git mv command to rename the file in both the working directory and the staging area:

git mv <old_filename> <new_filename>

Check the status the short way

git status -s

Useful diffs

Looking at the history

Viewing a commit

Look at the tree

Un-stage files

git restore --staged <filename> - un-stages a file from the staging area. git restore --staged . - un-stages all files from the staging area.

Remove files from the working directory

git restore <filename> - removes a file from the working directory. git restore . - removes all files from the working directory. git clean -fd - removes untracked files from the working directory. git checkout . - removes all files from the working directory.

Restore a file to a previous version

git restore <filename> - restores a file to the last commit. git restore <filename> --source <commit_hash> - restores a file to a specific commit.

Format the output

Setting up aliases

Checkout a commit

git checkout <commit_hash> - moves the HEAD to a specific commit. git checkout master - moves the HEAD to the master branch.

Check user history

git shortlog - shows the history of the repository by author. git shortlog -s - shows the history of the repository by author with statistics. git shortlog -s -n - shows the history of the repository by author with statistics and sorted by commits.

Check file history

git log --oneline --patch package.json - shows the history of the package.json file.

Restore a deleted file from a commit

git log --oneline -- <filename> - find the commit that deleted the file. git checkout <commit_hash> -- <filename> - restore the file from the commit.

blame

git blame <filename> - shows the history of the file with the author of each line.

Tags

Branching

Create a branch

git branch <branch_name> - creates a branch. git checkout -b <branch_name> - creates a branch and moves the HEAD to the branch. git switch -c <branch_name> - creates a branch and moves the HEAD to the branch. git swithc <branch_name> - moves the HEAD to the branch. git branch -m <new_branch_name> - renames the branch.

Delete a branch

git branch -d <branch_name> - deletes a branch. git branch -D <branch_name> - deletes a branch even if it has unmerged changes.

Compare branches

git log master..branch - compares two branches. git diff master..branch - compares two branches. git diff branch - compares the branch with the master.

Stash

git stash - stashes the changes. git stash push -am "message" - stashes the changes with a message. git stash list - shows the list of stashes. git stash show 1 - shows the last stash. git stash apply - applies the last stash. git stash apply stash@{1} - applies a specific stash. git stash pop - applies and drops the last stash. git stash drop - drops the last stash. git stash clear - drops all stashes.

Merge

There are two types of merges:

git merge <branch_name> - merges the branch into the current branch. git merge --no-ff <branch_name> - merges the branch into the current branch with a new commit.

See branches

git branch - shows the branches. git branch -v - shows the branches with the last commit. git branch -vv - shows the branches with the last commit and the tracking branch. git branch --merged - shows the branches that are merged. git branch --no-merged - shows the branches that are not merged. git merge --squash <branch_name> - merges the branch into the current branch with a squash commit.

Conflicts

Conflicts occur when two branches have changed the same part of the same file.

Undo a merge

git reset --soft HEAD - undoes the merge and keeps the changes. git reset --mixed HEAD - undoes the merge and keeps the changes in the working directory. git reset --hard HEAD - undoes the merge and removes the changes. git revert <commit_hash> - reverts a commit. git revert -m 1 <commit_hash> - reverts a merge commit on the first parent.

Rebase

Rebase is the process of moving or combining a sequence of commits to a new base commit.

Cherry-pick

Cherry-pick is the process of applying a commit from one branch to another.

Get a file from another branch

git checkout <branch_name> -- <filename> - gets a file from another branch. git restore --source <branch_name> -- <filename> - gets a file from another branch.

Remotes

git remote - shows the remotes. git remote -v - shows the remotes with the URLs.

Pull

git pull - fetches and merges the changes from the remote. git pull --rebase - fetches and rebases the changes from the remote.

Release

git tag -a v1.0 -m "Version 1.0" - creates a tag for the release. git push origin v1.0 - pushes the tag to the remote.

Sharing branches

git push -u origin <branch_name> - push and set the upstream.

Revert multiple commits

git revert <commit1_hash> <commit2_hash> - reverts multiple commits. git revert --no-commit HEAD~3 - reverts the last 3 commits. git revert --continue - continues the revert.

Recover a lost commit

git reflog - shows the history of the HEAD. git reflow show feature - shows the history of the feature branch.

Amend a commit

git commit --amend - amends the last commit. git commit --amend -m "New message" - amends the last commit with a new message.

Edit an earlier commit

git rebase -i HEAD~3 - starts an interactive rebase. Select the commit to edit and change the command to edit. git commit --amend - amends the commit. git rebase --continue - continues the rebase.

Drop an earlier commit

git rebase -i HEAD~3 - starts an interactive rebase. Select the commit to drop and remove the line. git rebase --continue - continues the rebase.

Reword an earlier commit

git rebase -i HEAD~3 - starts an interactive rebase. Select the commit to reword and change the command to reword.

Reorder commits

git rebase -i HEAD~3 - starts an interactive rebase. Reorder the commits.

Squash commits

git rebase -i HEAD~3 - starts an interactive rebase. Select the commits to squash/fixup and change the command to squash.

Split a commit

git rebase -i HEAD~3 - starts an interactive rebase. Select the commit to edit and change the command to edit. git reset --mixed HEAD^ - resets the commit but keeps the changes. git add <filename> - adds the changes. git commit -m "First part" - commits the first part. git add <filename> - adds the changes. git commit -m "Second part" - commits the second part. git rebase --continue - continues the rebase.

References