4 April 2024

git-commands-pocket-guide image

Git Commands Pocket Guide

Commands that we don't use frequently during the day, but are useful for Git, ensuring that the most helpful commands are always within reach.

# Undo the last commit, keeping changes staged.
git reset --soft HEAD^

# Update the last commit message.
git commit --amend -m "Updated commit message"

# Add all changes to the last commit without changing the commit message.
git add .
git commit --amend --no-edit

# Remove untracked files from the working directory.
git clean -f

# Unstage a file
git reset HEAD <file>
# or all files.
git reset HEAD .


# Push a new branch to the remote repository.
git push -u origin <branch_name>

# Apply changes from special stash commit.
git stash apply stash@{<index>}

# Apply changes and remove special stash commit.
git stash pop stash@{<index>}

# View commit history graph in one line.
git log --oneline --graph --decorate --all

# Stashing specific files or directories.
git stash push -m "message" -- <file_path>

# Showing changes over time for a specific file.
git log -p <file_path>

# Check whitespace errors.
git diff --check

# Comparing branches.
git diff <branch1>..<branch2>

Remove a file from a commits history:

git rebase -i <one_commit_before_the_commit_the_file_was_added>

# change 'pick' to 'edit'in the interactive rebase editor for the commit that added the file.

# remove the file from the staging area.
git rm --cached <path_to_file_to_remove>
# commit the changes.
git commit --amend
# continue the rebase.
git rebase --continue
# force push the changes to the remote repository.
git push origin <branch_name> --force