Comprehensive Git Cheat Sheet: Everything You Need to Know (Except Basic Git)

Jerry PM
4 min readJun 5, 2023

--

this article from my blog

Photo by Roman Synkevych on Unsplash

1. Git for deleting a branch

Deleted branch (branch name)


$ git branch -d <branc_name>
//example
$ git branch -d feature/main

2. Git for force checkout

ignore all code change branch feature/main

$ git branch -f <branch_name>

example
$ git branch -f feature/main

3. Git for `pick` the specific file use `checkout`

This can update specific files too like “cherry-pick” but just picking some file from another branch

$ git checkout origin/develop -- Main/Config/dev.xcconfig
$ git checkout origin/develop -- Home/HomeViewCOntroller.Swift

4. Git for Specific checkout commit

$ git checkout <commit>

Example:

$ git checkout 62c34cc
$ git checkout 6a4f3dc2

5. Git Branch Management

this git function to see in the branch

//To see list branch in your repo
$ git branch
// To see last commit on each branch
$ git branch -v

6. Diffrerent “git add .”, “git add -u” and “git add -A”

$ git add -A stages all changes

$ git add . stages new files and modifications, without deletions (on the current directory and its subdirectories).

$ git add -u stages modifications and deletions, without new files

Git Version 1.x

CommandNew FilesModified FilesDeleted FilesDescriptiongit add -A✔️✔️✔️Stage all (new, modified, deleted) filesgit add .✔️✔️❌Stage new and modified files only in the current foldergit add -u❌✔️✔️Stage modified and deleted files only

Git Version 2.x

CommandNew FilesModified FilesDeleted FilesDescriptiongit add -A✔️✔️✔️Stage all (new, modified, deleted) filesgit add .✔️✔️✔️Stage all (new, modified, deleted) files in the current foldergit add --ignore-removal .✔️✔️❌Stage new and modified files onlygit add -u❌✔️✔️Stage modified and deleted files only

Source: https://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add
Source: https://git-scm.com/docs/

7. When running git rebase get an error “fatal : No rebase in progress?

flow get that error in terminal

$ git add .
$ git rebase --continue
fatal: No rebase in progress?

the problem is because already in rebase

8. Undo the last git commit with a reset

When I forget to git push -f origin branch/name after rebase, for fixing that I need to commit in the terminal to undo git pull origin branch/name this code below.

$ git reset --soft HEAD~1

9. Cherry-pick

My code was saved by cherry-pick when in my office use rebase to track progress but cannot use
“git pull branch” so this helps me to solve that

- Old branch push to GitHub (feature/old)
- Next I’ll create new branch from the master ( git checkout -b feature/new)
- after that pick some commits from (feature/old) and use cherry-pick

$git cherry-pick dg1234a7
// I use this for pick one commit from other branch
// and add to my branch
  • and that how cheery-pick save my code from 1000x rebase :D

10. Git Remove Specific Commit

$git rebase -i HEAD~x

// Example I want to see last 5 commit
$git rebase -i HEAD~6

Change commit what you want an example like this, change `pick` to drop to commit want you remove. For edit press `i`

// from this
pick 1ed8ff6b92 commit number xx
pick cd9e5b0063 dadop
pick 117495e3d1 data x <- want to remove here
pick 05f9c6c553 Market Type
pick 1c61507391 fix key
pick c1150ca8bd last commit

// to this
pick 1ed8ff6b92 commit number xx
pick cd9e5b0063 dadop
drop 117495e3d1 data x <- remove here
pick 05f9c6c553 Market Type
pick 1c61507391 fix key
pick c1150ca8bd last commit

.
.
# d, drop <commit> = remove commit
.
.

After edit that quit and save this this vim use :wq and force push

$git push -f origin <branch>

example
$git push -f origin feature/abc

source:
https://stackoverflow.com/questions/2938301/remove-specific-commit

note git:
-f = force
-d = delete
-b = branch
-u = ?
-v = ?
-A = all

if you know what mean in that question mark comment below

--

--