Daily Workflow

8

questions

How do you find who changed a line and when?

git blame file.php shows, for every line, the commit, author and date of its last change; -L 10,20 limits the range. Then git show <hash> opens the whole commit with context. To find the commit that added or removed specific text — git log -S "text".

How do you unstage a file without losing changes?

git restore --staged file.php (in older versions — git reset HEAD file.php). The file goes back to modified, and the changes in the working directory stay.

What do you do if a secret has already been committed?

First, treat the secret as compromised and rotate it (password, token, key): it stays in the history and in every clone. Then stop tracking the file and add it to .gitignore. History can be cleaned (git filter-repo, BFG), but that rewrites it for the whole team — rotating the secret matters more.

What does git add -p do?

It shows changes in pieces (hunks) and asks for each one whether to stage it: y — yes, n — no, s — split into smaller ones. That way you can make several logical commits from one file and avoid committing stray debug code.

What makes a good commit message?

A short subject line of up to ~50 characters in the imperative mood that says what the commit does: Fix price rounding in cart, not fix or changes. If needed, a blank line and a body: why the change was made and what its consequences are. One commit — one logical change, and the message comes out precise.

What's the difference between git diff and git diff –staged?

git diff shows changes in the working directory that aren't staged yet. git diff --staged (synonym --cached) shows what's already staged and will go into the next commit. Before committing, --staged is the one to check.

When shouldn't you use –amend?

When the commit has already been pushed to a shared branch. Amend creates a new commit with a different hash: the history on the remote and on your teammates' machines diverges, and pushing requires force. Amend is only for local commits or your own branch nobody else works on.

Why is a file listed in .gitignore still tracked?

.gitignore only affects untracked files. If the file was already committed, Git keeps tracking it. You need to remove it from the index while keeping it on disk: git rm --cached file, and commit that change.