Git

Git Cheat Sheet

Bob Reselman
English

About

Cheap local branching, convenient staging areas, and multiple workflows are just a few of the features Git offers.  As an open source distributed version control system, there are many ways to use Git. The good news is getting started is easy.

This cheat sheet explains basic Git concepts and workflow and guides you through the processes for moving content to and from the remote repository. You’ll also learn how to merge files between branches, rebase files between branches, and invoke the  diff tool when merge conflicts occur.

Download the Git Cheat Sheet and learn commands around:

Working with repositories

  • git init
  • git clone
  • git pull
  • git fetch
  • git log

Working with branches in Git

  • git branch -r
  • git branch -a

Working with content

  • git status
  • git add
  • git commit
  • git push
  • git restore
  • git clean

Rolling back commits

  • git revert
  • git merge
  • git rebase
  • git mergetool
  • git blame
  • git tag

With Red Hat Developer cheat sheets, you get essential information right at your fingertips so you can work faster and smarter. Easily learn new technologies and coding concepts and quickly find the answers you need.

Excerpt

Rolling back to the most recent commit

The following sections describe how to merge files between branches, rebase files between branches, and invoke the  difftool when merge conflicts occur.

Example:

The following example shows the current branch as well as the files in that branch. The dev branch has two files,  newfile.txt and readme.md .

Then the branch is changed to main. The main branch has one file, readme.md . The command git merge dev --no-edit merges the files from the dev branch into the the current main branch. The option  --no-edit is used to avoid having to write a message describing the merge. Finally, the  ls -1 command shows that the merge successfully added  lnewfile.txt from the dev branch to main:

Merges the files and directories from <branch_to_merge_from> into the <target_branch>. If the <target_branch> parameter is not provided, the files and directories in the <branch_to_merge_from> are merged into the current branch.

$ git branch
* dev
main
$ ls -1
newfile.txt
readme.md
$ git checkout main
$ ls -1
readme.md
$ git merge dev --no-edit
Merge made by the 'recursive' strategy.
newfile.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newfile.txt
$ ls -1
newfile.txt
readme.md

git rebase

git clean [options] <filename> git rebase [options] <other_branch>

Merges one repository onto another while also transferring the commits from the merge-from branch onto the merge-to branch. Operationally, Git can delete commits from one branch while adding them to another.

Example:

The following example checks out the branch dev and then rebases the updates made in the branch  new_feature onto the branch  dev. The commits that were part of  new_feature are now part of  dev:

$ git checkout dev
Switched to branch 'dev'
 
$ git rebase new_feature
Successfully rebased and updated refs/heads/dev.