Common Git commands that I use and need to reference.
- Listed By Use Case - A situation (not precipitated by an error nor message) that I needed to search for a solution for.
- Listed (alphabetically) By Using a Git - Commands listed alphabetically by git command.
- Listed By Error Or Message - Solutions that I needed to seek out due to an error or message that I came across.
A situation (not precipitated by an error nor message) that I needed to search for a solution for.
issue: editing a commit message after push
source: The solution to Changing git commit message after push (given that no one pulled from remote); specifically I used
git commit --amend
- I edited the message in vi.
- lastly
git push --force <repository> <branch>, orgit push --force origin <branch>
This appears to be another good resource. How to change a Git commit message after a push
Of particular importance to me because I generated a lot with Dependabot pull requests.
I used
git branch --merged,git branch --merged| grep -Ev "(^\*|master|main|dev)", andgit branch --merged | grep -Ev "(^\*|master|main|dev)" | xargs git branch -d
source: How do I delete all Git branches which have been merged? Stack Overflow response.
command:
git mv OLD-FILENAME NEW-FILENAME
source: Renaming a file (Renaming a file using the command line)
You can rename any file in your repository directly in GitHub or by using the command line.
command:
git branch -m <oldname> <newname>
source: https://stackoverflow.com/a/6591218
See the Checking if the file is tracked in github section of the gitignore README file. Note, section name may change.
Commands listed alphabetically by git command.
Locally
git branch -d <branch>
Remote ones
git push remote_name -d remote_branch_name
git push origin -d remote_branch_name
source: https://stackoverflow.com/a/6591218
To rename a branch while pointed to any branch:
git branch -m <oldname> <newname>
To push the local branch and reset the upstream branch:
git push origin -u <newname>
To delete the remote branch:
git push origin --delete <oldname>
Here is a simple command that lists all branches with latest commits:
git branch -v
To order by most recent commit, use
git branch -v --sort=committerdate
Both from: JamieBort/Personal-Dashboard#23 (comment)
Related, see https://github.com/JamieBort/LearningDirectory/blob/master/Git/common_git_commands.md#for-each-ref
include #xxx in your commit message to reference an issue without closing it.
How to Close Reference issues and pull request GitHub Documentation
Stack Overflow post:
and:
close, closes, closed, fix, fixes, fixed, resolve, resolves, resolved
Referencing and closing issues across repos:
Git diff in specific file/directory while excluding files and directories Exclude a directory from git diff StackOverflow
git diff <one_branch>..<another_branch> -- path/to/directory/or/file
git diff <one_branch>..<another_branch> -- path/to/directory/or/file ':!<exclude_this_file_or_directory>' ':!<and_exclude_this_file_or_directory>'
git diff <one_branch>..<another_branch> -- ':!./backend/package.json' ':!./backend/package-lock.json' ':!./frontend/package.json' ':!./frontend/yarn.lock'
merging in only one file/directory
git checkout <other branch> <file_or_directory_from_that_other_branch>
merging in everything but one file/directory
Here is a simple command that lists all branches with latest commits:
git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'
Here is a simple command that lists all branches with latest commits:
From: JamieBort/Personal-Dashboard#23 (comment)
git rm --cached <path_to_file_name>
git rm -r --cached <path_to_directory_name>
example:
git rm -r --cached node_modules/
Solutions that I needed to seek out due to an error or message that I came across.
situation:
I attempted to merge main into dev: git merge main and received Already up-to-date.
The solution I used: https://stackoverflow.com/q/634546