Memos - Git helper

General

Status

git status

History

git log
git log --graph

Commits

Each commit is a snapshot of the project.

Git commit are local.

Simple commit

git commit -m "message"

Add to previous commit

git commit --amend

Show diff of a commit

git show <i>commit_id</i>

Branchs

List

git branch -v

New branch

The branch will start from the actual checkout/head

git checkout -b <i>branch_name</i>


 or 

git branch -c <i>branch_name</i>

git switch <i>branch_name</i>

Push a new branch for the first time

git push -u origin <i>branch_name</i>

Revert

Revert a unpushed commit

git reset --hard HEAD~1

Revert a pushed commit by its id

Warning: this will commit the contrary of the commit, this won't remove commit froim history.

git revert _commit_id_

Revert last pushed commits (1)

git reset --hard HEAD~1
git push --force-with-lease

Drop a commit from history

Just use drop for commits to forget (and use git push -f).

git rebase -i _commit_id_

Stash

Save changes with a specific name

git stash push -m "my_stash_name"

Restore last stash and remove it from stash list

git stash pop


Others

Squash

Use squash for all commits to merge.

git rebase -i _commit_id_

Git grep

git grep coin

Git history

See exact history, to be used to restore a specific commit

git reflog
git reset --hard #commit

Use cases

Remove Files From a Git Commit

Remove changes on some files in the previous (already pushed) commit


$ git reset --soft HEAD~1

$ git reset HEAD _file_

$ git checkout _file_

$ git commit ... 

$ git push --force-with-lease