Skip to content

Instantly share code, notes, and snippets.

@Dobby89
Last active April 26, 2024 12:36
Show Gist options
  • Save Dobby89/ffa86a2b6c259cc6535d340467a7a29a to your computer and use it in GitHub Desktop.
Save Dobby89/ffa86a2b6c259cc6535d340467a7a29a to your computer and use it in GitHub Desktop.
Git commands

Delete all local branches which have already been merged into master (or have no commits)

git branch --merged master | grep -v "master" | xargs -n 1 git branch -d

As a standalone function, Add the following to "C:\Users<user>.bashrc"

deleteOldLocalBranches() {
    git branch --merged master | grep -v "master" | xargs -n 1 git branch -d
}

In bash, type:

deleteOldLocalBranches

Get list of changes since a commit release

To get all the commits since the hash and copy a one-line summary of them all into your clipboard:

git log --oneline <commit_hash>.. | clip.exe

As a standalone function, Add the following to "C:\Users<user>.bashrc"

since() {
    git log --oneline "$1".. | clip.exe
}

In bash, type:

since <commit hash of current version on production>

e.g.

since af1c7fc

Revert the last local commit (before pushing).

Source: https://www.git-tower.com/learn/git/faq/undo-last-commit

git reset --hard HEAD~1

Change your username and email address for the current git project

USeful if you're on a machine with personal and work repos.

Set your username

git config user.name "FIRST_NAME LAST_NAME"

Set your email address

git config user.email "MY_NAME@example.com"

How to change the commit author for one specific commit

Source: https://stackoverflow.com/a/30737248/5243574

Github documentation contains a script that replaces the committer info for all commits in a branch (now irretrievable, this is the last snapshot).

  • Run the following script from terminal after changing the variable values
#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
  • Push the corrected history to GitHub:
git push --force --tags origin 'refs/heads/*'

OR if you like to push selected references of the branches then use

git push --force --tags origin 'refs/heads/develop'

How to modify existing unpushed commit messages

Source: https://stackoverflow.com/a/179147/5243574

Amending the most recent commit message

git commit --amend

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

git commit --amend -m "New commit message"

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

Changing the message of a commit that you've already pushed to your remote branch

If you've already pushed your commit up to your remote branch, then - after amending your commit locally (as described above) - you'll also need to force push the commit with:

git push <remote> <branch> --force

...or...

git push <remote> <branch> -f

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.

Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.

Perform an interactive rebase

Another option is to use interactive rebase. This allows you to edit any message you want to update even if it's not the latest message.

In order to do a Git squash, follow these steps:

Note: n is the number of commits up to the last commit you want to be able to edit

git rebase -i HEAD~n

Once you squash your commits - choose the e/r for editing the message:

Screenshot of terminal while editing commit

Important note about interactive rebase

When you use git rebase -i HEAD~n there can be more than n commits. Git will "collect" all the commits in the last n commits, and if there was a merge somewhere in between that range you will see all the commits as well, so the outcome will be n + .

Good tip:

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let Git resolve those conflicts automatically for you.

Documentation

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