Frequent Actions
Add to index
# add specific file git add file_name # add all files git add . # git add all types (incl. delete edits) git add -A
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
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
--trailer
git commit -m "Add support for X feature" \ --trailer "Reviewed-by: Alice <alice@example.com>" \ --trailer "Signed-off-by: Bob <bob@example.com>"
or you can just write in in new lines of the interactive commit message editor.
--allow-empty
Creates an empty commit with no changes
- when a commit without any change must be pushed to retrigger CI
- 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
- Changing the commit message of the latest commit
Modify the latest commit without creating a new commit
git commit --amend
- Modify the latest commit's message with a new commit message
git commit --amend -m "an updated commit message"