Skip to content
Hani fares edited this page Mar 25, 2022 · 18 revisions

Welcome to the git-cheat-sheet wiki!

Git as course:

  1. What is Git?
  2. Theoretical Git.
  3. How to install Git on your machine?
  4. Start using Git: Make a new project, create on file, then init a git repo. Then add it and commit it.

Creating Branches and switch between them.

Git log: to see and check all commits and Heads

Head: is the last commit in a branch.

Detached Head: is when you switch to one commit. this commit will be the detached head

Git ls-files: show all files in staging area

When you delete a file from working area, it still in staging area, you have also to delete it from the staging area.

To delete a file from the staging area, you have two ways:

  • git add that will add all existing files to the staging area (under the hood will delete the deleted file)
  • git rm <file_name> that will remove the delete file from the staging area

To delete the unstaged changes: git checkout/restore <file_name> // to delete unstaged changes in certain file git checkout/restore . to go back to last head.

To delete unstaged files: git clean -dn to see what would deleted. git clean -df to delete this file

Removing added chenges: there is two ways:

  • old one: with two commands: git reset <added_file_name> and then git checkout <added_file_name>
  • new way: with two commands: git restore --staged <added_file_name> and then git checkout <added_file_name>

Removing commited changes:

  • Soft remove: by running: git reset --soft HEAD~<NUM OF steps back> // Soft means that all files will stay in staging area.

Example: If we have a release Branch, ad you forget to create a new feature branch, and committed your changes to release branch, what will you do in this case?

First you go back to the last commit in release branch by ranning: git reset --soft HEAD~1/<last_commit_id>. Now the HEAD is pointed to the last commit, and your changes are in the staging area. So next to do is creating a new feature branch and then commit the changes to this branch and then merge it to release branch.

Delete Branch:

  • git branch -d <Branch_name>: delete just merged branches.
  • git branch -D <Branch_name>: Force to delete any branch.

Adding changes to an old commit (Detached HEAD): When you checkout to another old commit and then do changes th this commit and commit all changes, you have then to create a new branch and connect this changes to this branch by running: git branch <new-branch-name> <commit_id>

Command overview

  • git --version: Check installed Git Version
  • git init: create empty repository
  • git status: Check working directory & staging area status
  • git add <file_name> / .: Add single file or all files to staging area
  • git commit -m "message": Create a new commit
  • git checkout <commit_id>: Checkout commit (detached head)
  • git branch / switch <branch_name>: Create a new branch without go to the new branch
  • git checkout -b / switch -c <branch_name>: Create new branch and direct go to it
  • git checkout <branch_name>: Go to branch
  • git merge <other_branch>: merge branch with the other branch
  • git rm <file_name>: delete a file from staging area
  • git branch -D <branch_name>: Delete branch

Undo unstaged changes (Not added changes)

  • git checkout .: Revert changes to the last state (Just for tracked files)
  • git restore <file_name> / .: Revert changes to the last state (Just for tracked files)
  • git clean -df: Revert changes to the last state (Just for untracked files)

Undo staged changes (Added changes)

  • git reset <file_name> / . & git checkout / restore <file_name> / .: Removing files from staging area
  • git restore --staged <file_name> / . & git checkout / restore <file_name> / .: Removing files from staging area

Undo commited files

  • git reset --soft HEAD~1: Undo the latest(~1) commit.
Clone this wiki locally