Git Commands Cheat Sheet for GitLab and GitHub

 GitLab vs. GitHub

What is the difference between GitLab and GitHub?

GitLab is owned by GitLab Inc. and was created in 2011. It is an is an open-source DevOps platform.

Log in to GitLab.

 

GitHub was acquired by and is owned by Microsoft Inc. and was originally created in 2008. Git integrates seemlessly into the Microsoft ecosystem including Visual Studio and Visual Studio code editors.

Log in to GitHub.

 

GitLab Company Overview


GitLab Inc. is an open-source DevOps platform that provides a comprehensive suite of tools for software development, from project planning and source code management to CI/CD (Continuous Integration/Continuous Deployment) and monitoring. Founded in 2011 by Dmitriy Zaporozhets and Sytse Sijbrandij, GitLab has grown into a significant player in the DevOps and DevSecOps landscape. GitLab's platform enables teams to collaborate on projects in a single application, streamlining workflows and improving productivity. GitLab is known for its transparency and remote-first work culture, with employees distributed globally.

GitHub Company Overview


GitHub, Inc. is a web-based platform that hosts source code repositories and offers distributed version control using Git. Founded in 2008 by Tom Preston-Werner, Chris Wanstrath, PJ Hyett, and Scott Chacon, GitHub has become a central hub for developers to collaborate on projects. It provides various tools for code review, project management, and CI/CD. GitHub's user-friendly interface and social features have made it popular among both individual developers and large enterprises. In 2018, Microsoft acquired GitHub for $7.5 billion, integrating it further into the Microsoft ecosystem while maintaining its independence and open-source focus.

Git Commands Library

View the command library of git commands in alphabetical order.

git add

Use git add to files to the staging area.

git add <file_path>

You can recursively stage changes from the current working directory with git add ., or stage all changes in the Git repository with git add --all.

git blame

Use git blame to report which users changed which parts of a file.

git blame <file_name>

You can use git blame -L <line_start>, <line_end> to check a specific range of lines.

For example, to check which user most recently modified line five of example.txt:

$ git blame -L 5, 5 example.txt
123abc (Zhang Wei 2021-07-04 12:23:04 +0000 5)

git bisect

Use git bisect to use binary search to find the commit that introduced a bug.

Start by identifying a commit that is “bad” (contains the bug) and a commit that is “good” (doesn’t contain the bug).

git bisect start
git bisect bad                 # Current version is bad
git bisect good v2.6.13-rc2    # v2.6.13-rc2 is known to be goodgit bisectthen picks a commit in between the two points and asks you identify if the commit is “good” or “bad” with git bisect goodor git bisect bad. Repeat the process until the commit is found.

git branch

Use git branch to view all of the available branches.

See all available branches including both local and remote branches using the -a flag.

git branch -a

See all remote branches using the -r flag.

git branch -r

git checkout

Use git checkout to switch to a specific branch.

git checkout <branch_name>

To create a new branch and switch to it, use git checkout -b <branch_name>.

git clone

Use git clone to copy an existing Git repository.

git clone <repository>

Use git clone with the HTTPS link to checkout a remote repository through GitLab.



git clone <https://gitlab.com/account-name/branchname.git>

GitLab Checkout Branch HTTPS result

git commit

Use git commit to commits staged changes to the repository.
git commit -m "<commit_message>"

If the commit message contains a blank line, the first line becomes the commit subject while the remainder becomes the commit body. Use the subject to briefly summarize a change, and the commit body to provide additional details.

Use git commit --amend to modify the most recent commit

git commit --amend

git init

Use git init to initialize a directory so Git tracks it as a repository.
git init

A .git file with configuration and log files is added to the directory. You shouldn’t edit the .git file directly.

The default branch is set to master. You can change the name of the default branch with git branch -m <branch_name>, or initialize with git init -b <branch_name>.

git pull

Use git pull to get all the changes made by users since the last time you cloned or pulled the project.

git pull <optional_remote> <branch_name>

git push

Use git push to update remote refs.
git push

git reflog

To display a list of changes to the Git reference logs, use git reflog.
git reflog

git remote add

Use git remote add to tell Git which remote repository in GitLab is linked to a local directory.
git remote add <remote_name> <repository_url>

When you clone a repository, by default the source repository is associated with the remote name origin.

By default, git reflog shows a list of changes to HEAD.

git log

To display a list of commits in chronological order, use git log.

git log

git show

To show information about an object in Git, use git show.

For example, to see what commit HEAD points to:

$ git show HEAD
commit ab123c (HEAD -> main, origin/main, origin/HEAD)

git merge

To combine the changes from one branch with another, use git merge.

For example, to apply the changes in feature_branch to the target_branch:

git checkout target_branch
git merge feature_branch

git rebase

To rewrite the commit history of a branch, use git rebase.

You can use git rebase to resolve merge conflicts.

git rebase <branch_name>

In most cases, you want to rebase against the default branch.

git reset

To undo a commit, use git reset to rewind the commit history and continue on from an earlier commit.

git reset

git status

To undo a commit, use git status to show the status of working directory and staged files.