DEV Community

Orestis Pantazos
Orestis Pantazos

Posted on

🔧 Git: The Story Book

Git Cheat Sheet

Introduction

Git is a distributed version control system that supports collaboration through teams in the corporation. Teams of developers and open-source software maintainers basically handle and manage their projects through Git. It is very useful as open-source software toolkit for developers and DevOps engineers.

Git is ready to download it from the official website and is available for Linux, MacOS and Windows.

Setup Git

Check your Git version with the following command.

git --version
Enter fullscreen mode Exit fullscreen mode

Initialize the current working directory as a Git repository with the following command.

git init
Enter fullscreen mode Exit fullscreen mode

Clone your projects from hosted remotely Git repositories with specified repo's URL or server location on GitHub or GitLab instances.

git clone https://www.github.com/username/repo-name
Enter fullscreen mode Exit fullscreen mode

Show your current Git directory's remote repository.

git remote
Enter fullscreen mode Exit fullscreen mode

Staging

Check the status of your Git repo including files added that are not staged and the files that are staged also.

git status
Enter fullscreen mode Exit fullscreen mode

Include all the available files in the current working directory.

git add .
Enter fullscreen mode Exit fullscreen mode

Remove a file from staging within your working directory.

git reset CDI.java
Enter fullscreen mode Exit fullscreen mode

Committing

Commit the staged files with meaningful commit message that you can track your commits later on.

git commit -m "Added new files"
Enter fullscreen mode Exit fullscreen mode

Branches

Show all available current branches with the following command.

git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch on your working directory.

git branch new-branch
Enter fullscreen mode Exit fullscreen mode

Switch to another existing branch and checkout to your current working directory.

git checkout another-branch
Enter fullscreen mode Exit fullscreen mode

Create the branch and switch to the new one to your current working directory.

git checkout -b new-branch
Enter fullscreen mode Exit fullscreen mode

Merge the specific branch's history into the current one's.

git merge branch-name
Enter fullscreen mode Exit fullscreen mode

Collaborate and Update

Fetch and merge any commits from the tracking remote branch.

git pull
Enter fullscreen mode Exit fullscreen mode

Push your local branch commits to the remote repository branch.

git push origin main
Enter fullscreen mode Exit fullscreen mode

Inspecting

Display the commit history for the currently active branch.

git log
Enter fullscreen mode Exit fullscreen mode

Top comments (0)