Skip to content
Hani fares edited this page Mar 24, 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.

Clone this wiki locally