I’ve always used a git client to work with git. Originally this was Sourcetree, but in recent years has been the git client built in to rider. Whilst rider is excellent and very productive, not everything I work on is .net and I find myself increasingly in the terminal, fumbling around with git commands. I’m starting this year exclusively using git on the command line with the intention of getting much better at this!
This page is my little cheatsheet, listing the commands that I commonly use and saves me mincing around on the googles.
Clone repo (from github - using the Github CLI)
This clones the repo-name from my marcbeavan org on GitHub.
gh repo clone marcbeavan/repo-name
Stage changes (or “check in” if you are an old git)
git status - what has changed…
git status -s
$ git status -s M content/blog/my-dev-setup.md ?? content/blog/20240505-git-cheatsheet.md ?? static/images/blog/git/
This displays a list of unstaged changes, staged changes, and untracked files.
The -s
option gives a shorter, more concise output.
- ‘??’ are untracked
- ‘M’ indicates modified
- ‘D’ indicates deleted files.
Differences
To get a nice(ish) summary of the uncommitted differences. Nb. this doesnt include:
- Staged changes (files added with git add but not committed).
- Committed changes that are different from the remote.
git diff --color
To view the differences for a specific file…
git diff -- **/blog.md
Add the new things
This can be done item by item.
$ git add static/images/blog/git/ $ git status -s M content/blog/my-dev-setup.md A static/images/blog/git/git-cheatsheet-banner.webp ?? content/blog/20240505-git-cheatsheet.md $ git add content/blog/20240505-git-cheatsheet.md $ git add content/blog/my-dev-setup.md $ git status -s A content/blog/20240505-git-cheatsheet.md M content/blog/my-dev-setup.md A static/images/blog/git/git-cheatsheet-banner.webp
or if everything needs adding…
git add .
$ git status -s M content/blog/my-dev-setup.md ?? content/blog/20240505-git-cheatsheet.md ?? static/images/blog/git/ $ git add . $ git status -s A content/blog/20240505-git-cheatsheet.md M content/blog/my-dev-setup.md A static/images/blog/git/git-cheatsheet-banner.webp
Undo
Can rollback or unstage files that you’ve added to the staging area in Git, you can use the git reset
$ git reset $ git status -s M content/blog/my-dev-setup.md ?? content/blog/20240505-git-cheatsheet.md ?? static/images/blog/git/
Commit the changes (locally)
Once you’re all happy with your staged changes, you can commit them:
git commit -m "a useful commit message, definitely not just a '.'"
Push changes to the remote.
$ git push
Commit history
one liner summary
$ git log --oneline -n 5
commits by user
$ git log --author="Marc Beavan" --oneline
commits since date
$ git log --since="2025-01-01" --oneline
commits on specific file
$ git log -- **/*git-cheatsheet.md
Files touched by a commit
$ git show --name-only 95f2a9d
Branches
List branches
Get the list of branches, ordered by most recently used.
> git branch --sort=-committerdate * master develop rc Docker
Switch to a branch
> git switch develop
Create a new branch and switch to it…
> git checkout -b MyNewBranch
Git-Extras
The git-extras package is a collection of additional utilities and convenience commands that extend the functionality of Git. It is not part of the standard Git distribution but is created and maintained by the community. The tools and commands provided by git-extras help with various aspects of managing Git repositories, making it easier to perform some common tasks that are not directly covered by the basic Git commands.
Useful commands
git ignore
: Easily add new entries to the .gitignore file.
$ git ignore ".hugo_build.lock"
git setup
: Initialize a new Git repository, adding all the files.
$ git setup
git summary
: Show a summary of the repository including the number of commits per contributor.
$ git summary project : BobMcBobber repo age : 11 months branch: : main last active : 7 hours ago active on : 11 days commits : 12 files : 304 uncommitted : 1 authors : 12 Marc Beavan 100.0%
-
git changelog
: Generate a changelog based on commit history. Outputs it as a bulletted list in vim. If you use tags then it will give the changelog since the last tag. -
git undo
: A quick way to undo the last commit (without removing changes).
$ git undo
can also undo the last n commits.
$ git undo 3
git obliterate
: Splats a file from the repo, including all history.
$ git obliterate myPassword.txt
git archive-file
: Creates an archive of the current repo.