Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Last active September 22, 2024 08:01
Show Gist options
  • Save aneurysmjs/070209e06652dcc2514f8b8ef74f3731 to your computer and use it in GitHub Desktop.
Save aneurysmjs/070209e06652dcc2514f8b8ef74f3731 to your computer and use it in GitHub Desktop.
git tricks

ignore files temporarily

start ignoring changes to a file:

git update-index --assume-unchanged path/to/file

keep tracking again:

git update-index --no-assume-unchanged path/to/file

see here

get a list of files marked --assume-unchanged:

git ls-files -v | grep '^[[:lower:]]'

list files in a commit

see here

git diff-tree --no-commit-id --name-only -r 'hash of the commit'

--no-commit-id suppresses the commit ID output.
--pretty argument specifies an empty format string to avoid the cruft at the beginning.
--name-only argument shows only the file names that were affected (Thanks Hank).
Use --name-status instead, if you want to see what happened to each file (Deleted, Modified, Added)

-r argument is to recurse into sub-trees

add missing file to last commit

see here

git add the_left_out_file
git commit --amend --no-edit

fetch main and merge without changing branch

git fetch origin main
git merge origin/main

How do I change the author and committer name/email for multiple commits?

see here

Using Rebase

NOTE: This answer changes SHA1s, so take care when using it on a branch that has already been pushed. If you only want to fix the spelling of a name or update an old email, git lets you do this without rewriting history using .mailmap. See my other answer.

Using Interactive Rebase

git rebase -i -p <some HEAD before all of your bad commits>

Then mark all of your bad commits as "edit" in the rebase file. If you also want to change your first commit, you have to manually add it as the first line in the rebase file (follow the format of the other lines). Then, when git asks you to amend each commit, do

git commit --amend --author "New Author Name <[email protected]>" 

edit or just close the editor that opens, and then do

git rebase --continue

to continue the rebase.

You could skip opening the editor altogether here by appending --no-edit so that the command will be:

git commit --amend --author "New Author Name <[email protected]>" --no-edit && \
git rebase --continue

Single Commit

As some of the commenters have noted, if you just want to change the most recent commit, the rebase command is not necessary. Just do

git commit --amend --author "New Author Name <[email protected]>"

This will change the author to the name specified, but the committer will be set to your configured user in git config user.name and git config user.email. If you want to set the committer to something you specify, this will set both the author and the committer:

 git -c user.name="New Author Name" -c [email protected] commit --amend --reset-author

Using git reset

git reset

How it works: • It changes the current branch (HEAD) to point to a different commit. • It can modify the staging area and working directory depending on the options used.

Let's check what option git reset offers us:

--soft: Moves HEAD to the specified commit but leaves the staging area and working directory unchanged.

--mixed (default): Moves HEAD and resets the staging area to match the specified commit; the working directory remains unchanged.

--hard (yolo): Moves HEAD, resets the staging area, and resets the working directory to match the specified commit. All changes after that commit are lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment