HomeToolsAbout a20k

Frequent Actions

Add to Index

# add specific file git add file_name # add all files git add .

Remove from Index

Prefer git rm over other methods

rm removes file or directories from both the staging index and the working directory which is preferred over deleting the file in the file system and having Git detect the deletion

rm will always lead to cleaner diff

git rm deleted_file_name # remove from index only, keep file in filesystem git rm --cached file_name

Move/Rename Files

Always prefer rename over deletion when moving files

For example, without using rename, file rename will register as a deletion and addition of file

  • With rename, the addition and deletion is combined as a single action of renaming path

Use git mv as first class

Renaming files

git mv old_name new_name

Moving file

git mv old_path/file_name new_path/file_name

Move files in a directory to new location

git mv old_path/folder_name/* new_path/folder_name/.

git restore

# unstage file_name from index git restore --staged file_name # ## Reset the file back to original state after unstaging git restore file_name

Safely reset file to another commit or branch name

git checkout commit_hash -- file_path

git commit

--allow-empty

Creates an empty commit with no changes

  1. when a commit without any change must be pushed to retrigger CI
  2. folding in small code change into previous commit
git commit --allow-empty -m "fix CI"

--no-verify

Can be used to skip pre-hooks

git commit --no-verify

git amend

Use Case

  1. Changing the commit message of the latest commit

Modify the latest commit without creating a new commit

git commit --amend
  1. Modify the latest commit's message with a new commit message
git commit --amend -m "an updated commit message"
© VincentVanKoh