DEV Community

Cover image for Git Quick Reference | The Comprehensive Guide to Git
Ayobami Adejumo
Ayobami Adejumo

Posted on

Git Quick Reference | The Comprehensive Guide to Git

Git is a Version Control System. A version control system is a software designed to record changes within one or more files over time.
When developers work on a project, different files are created and changes are made continuously to parts of the code. As the project grows, some of the code results in conflict. Git enables us to roll back, revert or cancel pending changes within one or more files. Git also enables us to compare the changes made to the different versions of a project. Having understood the importance of git, let's dive in

Installation

If you don't have git already installed on your machine, you can download the latest version from the git-scm website https://git-scm.com/downloads according to the version of your operating system - Windows, Mac, or Linux.
if you're running a Windows operating system, download the Windows version and go to the DOWNLOAD folder in file explorer.
Now double-click to install and follow the prompt. once installed, let's proceed to set up.

Configuration

You can confirm if you correctly installed git with the git --version On confirmation, let's proceed to configure our name and email.

git config --global user.name "firstname lastname"
git config --global user.email "firstname@email.com"
#check this 
git config --global --list
Enter fullscreen mode Exit fullscreen mode

Getting Started

To learn the basic git commands by practice, open the terminal of a text editor such as Vscode and practice with the following commands.

git init
Initialize a new git repo in the current working directory. Without this command, most of the commands below won't work.

git clone [https://yourremoterepo...]
This command helps to download a project from a remote repository. git clone copies the entire repository including branches and commit

git status
Displays the state of the working directory. It displays the files which are staged, unstaged or untracked. This command doesn't make any changes or modify any file.

git add filename
Add file from the working directory to the staging area

git add .
recursive add lets you add all untracked files in the working directory to the staging area.

git commit -m "enter your commit message here"
Create a new commit from changes added to the staging area. The commit must have a message!

$ git rm [filename]
Remove the file from the working directory and staging area.

Reviewing Commit History

$ git log
Used to view the history of committed changes within a repository. git log displays the history of everything.

$ git log --oneline --graph --decorate
Display a text-based graphical representation of the commit history. It shows an overview with reference labels and a history graph.

$ git show commit-id
Show commit log with information such as author, date, and diff output

git diff
show changes between the working directory and the staging area. This helps you track and compare the various changes you've made to code.

git diff --staged [file]
Shows any changes between the staging area and the repository.

Revert or Undo Changes

git checkout [filename]
Discard changes in the working directory. This operation is unrecoverable.

git reset commit-id
Revert your repository to a previously known working state.

git revert
Undo changes to a repository's commit history

$ git revert [commit sha]
Create a new commit, reverting changes from the specified commit.
It generates an inversion of changes.

Git branching model

$ git branch -a
List all local branches in a repository. With -a: show all branches (with remote).

$ git branch [branch_name]
Create a new branch, referencing the current HEAD.

$ git checkout [branch_name]
Switch the working directory to the specified branch.

$ git checkout -b [branch_name]
Create a new branch and switch the working directory to the new branch.

$ git merge [branchname]
Join specified [branchname] into your current branch (the one
you are on currently).

$ git branch -d [branchname]
Remove the selected branch, if it is already merged into any other.

$ git reflog
List operations (e.g. checkouts or commits) made on the local repository.

Tagging

$ git tag
List all tags.

$ git tag [tagname]
Create a tag reference named name for the current commit.

$ git tag -d [tagname]
Remove a tag from the local repository

Synchronizing repositories

$ git fetch [remote]
Download changes from the remote, but not update tracking branches.

$ git fetch --prune [remote]
Delete remote Refs that were removed from the remote repository.

$ git pull [remote]
Fetch changes from the remote and merge the current branch with its upstream.

$ git push [--tags] [remote]
Push local changes to the remote. Use --tags to push tags.

Stashing

$ git stash
Put current changes in your working directory into the stash for later use.

$ git stash pop
Apply stored stash content into the working directory, and clear stash.

$ git stash drop
Delete a specific stash from all your previous stashes

Top comments (0)