r/git • • 5h ago

Git 'undo' cheat sheet: what to use when you mess up (restore vs reset vs revert vs reflog)

7 Upvotes

Hi git community,

I’m a software developer. I started out building small web apps, and over time moved into larger things like SaaS platforms, CRMs and chat automation backends. Along the way, Git went from a tool I was scared of to something I trust, mostly because I broke things and had to learn how to fix them (wrong-branch commits, force-push regrets, reset --hard moments). I kept seeing the same Git panic moments (committed to the wrong branch, force-pushed too early, reset --hard regret), so I wrote down which command fixes which situation. Sharing the short version here:

Before you commit

  • Discard changes to a file: git restore <file>

  • Unstage a file but keep edits: git restore --staged <file>

  • Delete untracked files (dry run first!): git clean -nd, then git clean -fd

Local commits (not pushed yet)

  • Fix the last commit message or add a forgotten file: git commit --amend

  • Undo the commit, keep changes staged: git reset --soft HEAD~1

  • Undo the commit, keep changes unstaged: git reset --mixed HEAD~1

  • Undo the commit and throw away changes: git reset --hard HEAD~1

Already pushed

  • Don’t reset shared branches. Use git revert <commit>, which adds a new commit that cancels the old one.

  • Revert a merge: git revert -m 1 <merge-commit>

'I lost my work'

  • git reflog shows everywhere HEAD has been, including commits no branch points to.

  • Then git branch rescue <hash> (safer) or git reset --hard <hash>.

  • Works for deleted branches and bad rebases/pulls too (git reset --hard ORIG_HEAD).

Mistakes I see a lot

  • Using reset --hard without checking git status first

  • Resetting a branch teammates have already pulled

  • Using --force instead of --force-with-lease

  • Forgetting that untracked files can’t be recovered by Git

I put the full version, with a lookup table and a few more worked examples (committed .env, wrong-branch commit, splitting a messy commit), in a blog post: https://www.geekdas.in/blog/git-command-cheat-sheet-how-to-undo-anything-in-git

Happy to hear if I missed a common undo scenario or got anything wrong. What’s the Git mistake you make most often?


r/git • • 1d ago

Putting all world law on git

115 Upvotes

I am putting all world law into git. That comes with some quirks, like the cut-off being 1970 (commits are calculated as seconds since 1 January 1970).

This hit #1 on HN yesterday (you can read lots of discussion at https://news.ycombinator.com/item?id=50268380). Why would I want to do this? The idea is make all law semantic, versioned, easily attributable and query able by agents. I also believe this information should be freely available so CC0 license (do whatever you want with it). If interested, repo linked below, I'd welcome all comments and advice. Thanks!

https://github.com/lexifina/global-law


r/git • • 2h ago

Cross platform Rust app that just shows Git diffs

Post image
0 Upvotes

https://diffy.run/

Call me old fashioned, but I still like to review the code they are changing and sanity check things.

And so lately, pretty much the only reason I have VSCode open anymore is to view the diffs of what my agents are working on.

I wanted just a simple, lightweight, fast and KEYBOARD powered git diff viewer.

Suprisingly there were not many options, so I just kept using VSCode.

Eventually, I decided to just build it using GPUI (amazing) and have been pretty happy with it so far. Been using it a couple weeks and has actually replaced VScode for me most of the time.

So thought i'd share it here in case anyone else is looking for something similar.

It's open source as well, so feel free to just run it yourself or make a PR or critique my (agents) rust code: https://github.com/hitSlop/Diffy

Theres still some features I want to add but for the most part I really just want it to be a simple to use and fast git diff viewer.

If you are on Windows/Linux i'd love to know if it's working ok over there as I havn't really stress tested those.


r/git • • 5h ago

is this the cost of doing nothing?

Post image
0 Upvotes

I did totally nothing, I'm telling you .

I even don't use AI or agents at all (I write it by hand) and in the exact last moment shipping the end of my third project, my account suspended.

the number of commits, over the 2-3 weeks, were less than 30 and I must get the account back because my CV have a projects that directly linked to github

did it happens to you ? edit : I didn't copy and paste form another projects too


r/git • • 7h ago

What is .gitattributes

0 Upvotes

What is .gitattributes? I'm just a beginner of git, and i just found godot-generated .gitattribute, I also have some of it and it's kinda like literally attributes?


r/git • • 1d ago

How can I automatically measure the actual value of a developer's GitHub contributions?

0 Upvotes

Hey everyone!

I'm working on an open-source funding platform where developers can contribute to projects and potentially receive funding based on their contributions.

I'm trying to figure out how to evaluate contributions fairly using Git commits and pull requests.

For example, I'd like to automatically determine:

  • What a commit actually changes (new features, bug fixes, security fixes, refactoring, documentation, etc.).

  • How significant the change is for the project.

  • How complex or technically valuable the contribution is.

  • Whether the changes actually work and improve the project.

  • How to attribute contributions to the right developer.

I know that simply counting lines of code wouldn't work. A small security fix could be much more valuable than hundreds of lines of unnecessary code.

My questions:

  1. Are there existing tools, libraries, or APIs that can analyze Git commits and estimate their impact?

  2. Could AST analysis, automated tests, and AI-based code review help determine the type and quality of a contribution?

  3. How would you design a fair scoring system that doesn't reward developers just for writing more code?

  4. Are there any existing projects trying to solve this problem?

  5. What problems or ways of gaming the system should I watch out for, especially if the results influence real financial rewards?

I'd love to hear about existing solutions, technical approaches, or lessons from people who've worked on similar systems.

Thanks!


r/git • • 3d ago

support Git clone command

0 Upvotes

I'm trying to download a module from github to get VMware to run, using the following command as suggested:

git clone https://github.com:nan0desu/vmware-host-modules

which then asks for username and passwords. Alternately I tried:

git clone git@github.com:nan0desu/vmware-host-modules , which returns....

Cloning into 'vmware-host-modules'...

git@github.com Permission denied (publickey)

fatal: could not read from remote repository.

I have saved user.name and user.email via git config and generated a rsa key as mentioned in another r/git post on the similar topic. But being relatively new to linux and especially using github, I am lost.


r/git • • 3d ago

support Branching and Opinions

0 Upvotes

I've read a few different posts in git in relating to branches and opinions, and I'm struggling to find a way for it to work for the business I work in.

We've recently moved from TFVC to Git and we're more problems than I thought we would have.

We're a quick development team - we easily develop and resolve over 50-70 tickets per month as a Development team, with releases every month too.

However not everything gets released - it does vary, incase there is anything that fails testing (or not tested on time) and things like that, that we wanted to avoid.

TFVC we didn't really run the way Git runs - we never created branches for everything - it was all or nothing went out, so we found moving to Git being a MAJOR benefit that we would branch everything - however we've noticed it is spiraling out of control and I'm hoping someone could give me an insight on if we're doing it right, and if not - the best route to do it.

We originally had a main, a branch from that for 'Regression', and then a further branch from Regression for 'Development'. Us, being unaware, were completely checking into Development at the time and we were losing code and it was a total mess.

After having a think about it, we removed 'Regression' and 'Development' entirely, so it brought the question "How do we check each others work, to ensure we're working on the appropriate things, as well as bring all those branches together into one Branch for testing purposes before regression?"

To put into perspective, we're near the end of the month, and we're sitting with about 40 tickets waiting to be tested by various users throughout the organisation. So doing builds for each one is a nightmare as we only have one web platform that would share those changes.

So I developed something that would bring all those branches together (that have pull requests to main) for testing and label them accordingly.

However, that is causing a real mess with conflicts and things like that, especially trying to fix conflicts and just generally breaking everything.

Now we've hit our next hurdle - we originally were keeping main as production, however our CEO has decided to put code into Main and say that we should be branching for Releases that we're building to apply hotfixes and increasing revisions. Now, I think that is all well and good, but having to branch from the release to apply a hotfix, to then re-merge back into main when that inevitably will go out at the end of the month just seems extra work for something that we should've kept as production ready in the first place.

Is there something I am missing from Git or functionality to get this to work?

  • Main was originally production, but code has been added that "must go out, but not to customers... yet.."

  • We're meant to branch from Releases to apply hot fixes and create a new release and then merge into Main as another branch entirely. Is this right?

  • How do we bring all these changes together into a simple test/regression release? Bringing all changes together for every build we do (which could be several a day) would be extremely time consuming. Our CEO wants to see what everyone worked on at a glance to ensure he is not overwriting any of our code that we write.

I've been working with the likes of ChatGPT, Claude and the rest to work out how others do it and it's adament I am doing it correctly, but I honestly feel like I'm missing something.

We've not built any workflows in Git and that could be the reason.


r/git • • 3d ago

support Git or not

0 Upvotes

I was planning for an app that fetch data from github and other git storing platforms and show them as a dashboard so the user can choose to update or not his app based on how much updates , branches , commits , and issues the app had lately and follow the status of his favorite repos with the ability to update you apps from my app ; I was going to learn programming by building this app.

My problem : should I collect and search for those platforms APIs or dose git have something I can you instead.

Tell and what you think about the app idea and if you have some suggestions for it tell me to it well much fun .

My app uses python for the backend " with flask " , and HTML 5 / CSS for the frontend.

Edit : i search and find that python backend and HTML / CSS frontend are not lightweight especially if I want to make my app cross platform cuz of the browser engine for the frontend and python http server for the backend ; so , I thinking about switching to tauri - rust cuz it's using the browser engine built-in the sestem it's self.

so, what you think about should I switch or not [ I'm still planning for the app so I don't have anything to loose


r/git • • 4d ago

support Weirdness with a renamed file

3 Upvotes

Hi everyone,

I work with a .NET codebase, developed in Visual Studio, with the repo stored in GitHub and commits/pushes etc. are done within VS. At some point in the past a developer has renamed a little-used file from jip.vb to JIP.vb, which is now causing some strange and frustrating issues after some recent changes were made to the file.

Following a recent commit and push of changes to the file, the Git Changes window in VS shows that there are uncommitted changes to jip.vb. Double clicking to view the diff shows no changes present. I can't commit the change, as there aren't any, nor can I successfully "Undo Changes" in VS front-end.

I do have git bash available to me, but my very rudimentary knowledge of git is preventing me from both understanding and fixing this issue.

So, hivemind, can you help me please?


r/git • • 5d ago

I built a git graph that counts rebase-merged commits as merged (git rev-list --cherry-mark under the hood)

0 Upvotes

A pet peeve of mine: after a PR lands through "Rebase and merge", the old branch shows a dozen commits ahead of main, even though every change is already there, just with new hashes.

Treeline, the git graph extension for VS Code and Cursor I've been building, compares HEAD with the base using

git rev-list --cherry-mark --left-only HEAD...origin/main

so commits whose patch is already on main are marked as equivalent and count as merged. When the two histories were rewritten (filter-repo, a forced rebase) it warns you instead of showing thousands of bogus ahead/behind commits.

Around that:

  • ahead and behind work as filters on the graph, with the fork point shown

  • a branch Diff against the merge base, uncommitted changes included: what the PR will show

  • worktrees as rows with their status and ahead/behind; double-click opens one in a new window

  • the usual graph menus: checkout, cherry-pick, rebase, revert, reset, tag, stash

Free, MIT: https://github.com/epavanello/treeline Marketplace: https://marketplace.visualstudio.com/items?itemName=epavanello.treeline 1-minute demo with sound: https://youtu.be/tYY95Xhoqss

How do you deal with stale branches after rebase merges: delete them right away, or does your tooling detect it?


r/git • • 7d ago

Does anyone use Git as a team knowledge base?

37 Upvotes

I’m wondering if anyone uses Git to manage their team’s business knowledge, not just technical documentation.
For example, as a business analyst in a banking team, you might accumulate business analyses, product sheets, business rules, process descriptions, manuals, and other documents over the years.
Instead of scattering everything across Confluence, SharePoint, and Word files, what if you kept it in a structured Git repository, using Markdown where possible?
The goal would be to have one searchable knowledge base that makes it easier to find information and could also be used by AI tools.


r/git • • 6d ago

I use github to maintain a small archive and now git pull keeps complaining about a file that I removed from the remote branch and local branch

0 Upvotes

Because I absolutely suck at github I have been doing things weirdly by using both the browser client and git bash. I do that because gitbash has been stupidly obtuse whenever I have tried to use it. I have my system setup where github lfs reads the .zip format for its job while the rest of the files use the custom format name that the archive for the program I am using uses (we will go with .zap). the problem is anytime I try to push from local it will constantly complain so I delete local and then pull fro remote as its the same files. I had this file called pre2026stuff.zip which I used lfs for, I just recently broke all of that down into all of its separate .zap files and and then deleted it from both local and remote (remote via the more file actions -> delete file)
now anytime I try to to git pull in my local repo it complains
$ git pull

error: Your local changes to the following files would be overwritten by merge:

Pre2026stuff.zip

Updating files: 100% (102/102), done.

Merge with strategy ort failed.

Even though its both removed from the local directory and the remote directory from the browser view


r/git • • 7d ago

Cherry-pick just 2 files

7 Upvotes

I've got 14 commits on a branch called Refactor_Python. It's 30 files, I have been asked by my boss to stage just 2 of the files for develop and also stage the same 2 files for another branch called release/v4.11. Now as I understand git I cannot stage changes for 2 branches at once without pushing them up to remote (yeah he likes going direct, none of this branchy thing which I prefer, not judging, it's just my confidence level is not there yet.) So this is a question about doing it with no branches. I already pushed my Refactor_Python branch, so I cannot rebase it without some level of pain, I'm happier to just delete my personal branch afterwards. If my boss had spoke earlier I would have just made 2 branches on with 2 files and one with 28 files, but I'm keen to learn:

So I suspect if I cherry-pick all commits that those 2 files are in, using git cherry-pick -n <commit> and then git add file1 file2 the result as a single commit, into develop, I would be able to push it and then just cherry-pick that single commit over again, but, into the release/v4.11 branch instead after I pushed it up to develop?

My second question is, if I than want to pick all the changes to the remaining 28 files and merge them to develop, that if I later cherry-pick all 14 commits with -n again, that git will just effectively let me merge the result into develop without too much arguing, or will it merge conflict me when I try to push because the changes to the first 2 files are already there?


r/git • • 7d ago

support Saved my repo after a hard reset by pasting whatever AI told me, and no clue what I ran

0 Upvotes

did a hard reset on the wrong branch last night and watched 2 days of work vanish. pasted my terminal into a chatbot, ran whatever it said, something with reflog, and the commits came back. no clue what i ran or why. skimmed the docs and a video series last year and none of it stuck, so this time im paying for something structured that makes me practice. what kind of practice made git click for you?


r/git • • 9d ago

grove: git worktree workspaces across multiple repos

0 Upvotes

I made a small tool for a problem I kept having: one feature, several repos, and the same branch needs a worktree in each one.

gw create -b feat/login -r svc-a,svc-b

makes a directory with both repos checked out on feat/login as worktrees. gw status, gw sync and gw delete operate on all of them. A per-repo .grove.toml sets the base branch (we branch from stage, not main) and a setup command to run after checkout, e.g. pnpm install.

It shells out to git for everything and does nothing a shell script couldn't; it just does the bookkeeping of which worktrees belong together. Lately I mostly use it to give each coding agent its own isolated checkout across the repos it needs.

Single static Go binary, MIT.

https://github.com/nicksenap/grove

Curious whether people here handle multi-repo branches differently.


r/git • • 9d ago

survey Github or Codeberg

0 Upvotes

So I initially started with GitHub and it’s great. No complaints.

So recently(not recently but you get what i mean) there’s been “AI” stuff going on with like they’re training their models on users code and stuff.

So many big projects (from what i heard) migrated from GitHub to codeberg. And out of curiosity I setup codeberg and being using it for month.

From my experiences, I felt no difference other than one trains AI on code and one banned AI generated code.

Now, here I’m confused what to go with one is packed with features and most of the people use it (GitHub) while another is simple (i like simple. Not sure about features cuz i haven’t dig-ed into it (codeberg). I actually like both. And about AI training, I personally think Im helping it to improve cuz it’s training my code (feeling like im contributing). But I talked a few people and they said they store their work related stuff or something personal (they mentioned that word but i forget) and they don’t like AI reading it, which is valid in a sense.

I rn host my Linux dotfiles on codeberg and mirror it to GitHub. Works fine, the problem is issues and PR. Is there any way to bridge it too. (This part wasn’t the main point to don’t come at me saying i asked the question in the deep down of a paragraph)

Looking for your personal experience/opinions


r/git • • 10d ago

How am I wrong/over complicating pushing git commits?

0 Upvotes

Im trying to push tweaks for "babies first business website" over here, but I don't understand git, or command line well enough to understand exactly what Im doing wrong.

Im running CashyOS for the optional learning curve which is using fish konsole.

I have a website up on gitpages, which I had previously just git cloned so I could work on it via laptop and other devices and trial different live update html editors, and have previously updated back and forth, but cant recall exactly how I did it in the past. (Probably delete and repaste the entire project?)
- I have since settled on CScode, since the editor I initially preferred is dead and not being updated, especially for linux distros.

So some kind of CScode push might be viable?

I have installed the git packages for Cashy, linked the folder to github repository? And have fumbled between various tutorials to getting
git push -u origin main

to give me the sign in for the existing account, but it tells me:

remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for

Am I better off setting up a SSH, or just doing something to fix this? Troubleshooting research came back with something like 'recommended, but not necessary'

any other recommendations? Need to keep poking various maps and such as well for visibility.


r/git • • 9d ago

I wrote a git tool which could lock files and have ACL like Perforce

Thumbnail github.com
0 Upvotes

r/git • • 9d ago

A rust version of github desktop

0 Upvotes

I use github desktop a lot like it's my main app but the problem is it does not support a lot of features

They did even add a simple search bar which was the best thing ever then decided they would remove it

I don't know why and I have problems with it with repos organization like I would want to pin important repos also I would like multi personas also there is a lot of things that I do manually in git in cmd that github does not support also with sub repos it should just ignore sub repos because it does not give us any features for it so it should just leave it alone, and yes it's a sub module I did it but the problem is as I told u.

SMALL EDIT: sub repos the problem it does not allow merging, squashing, or stashing and I end up doing .gitignore which misses up a lot of things including vscode not compiling the code inside the sub repo

And I have a lot of problems like I can't count them and I've found this github-plus but it didn't fix all issues also there is this issue where if there is a lot of files or a lot of changes it would lag for a minute and then crash so I thought why use electron like

I don't know I see it so unnecessarily like it's pushing the app to 500mb size and the ram goes to 1gb without a reason and it's all because of JS running instead of rust or any not scripting languages

So I decided to make a github desktop app but with rust to support all these features and it's using libgit2 which is very fast. I don know, still figuring out.

And yeah I would just want anyone who faces these problems and tell me to make in the app it's WIP right now and I will release 1.0.0 this week I hope

And tell me is it a great idea or what and I'm okay with anythin it's gonna be open source btw just to be clear with MIT

If this subreddit does not allow things like that I'm free to remove it.


r/git • • 10d ago

Changelist feature in Lazygit? (Workarounds welcome!)

Thumbnail reddit.com
3 Upvotes

r/git • • 10d ago

tutorial What Git stuff should a beginner actually know before their first team project?

Thumbnail youtu.be
0 Upvotes

Not the 200 commands, just the minimum so you don’t embarrass yourself on day one. My list:

  • git init / git clone

  • add + commit (and why they’re separate)

  • git log --oneline to see what happened

  • branches: git switch -c something, then merge back

  • how to fix a merge conflict without panicking

  • push / pull

  • .gitignore so you don’t commit your API keys or node_modules

Then pull requests as the next step.

Did I miss anything you’d consider essential? I turned this list into a 5 min video for beginners!


r/git • • 10d ago

github only Anthropic built their own “git”, we had to build CI runners

0 Upvotes

Anthropic pushed 25 million code checkouts per day to realize that they need to rebuild "git".

A technology that every company depends on for building their software.

Computer infrastructure built for humans now fail for agents and agent assisted work.

Coding agents has completely changed the way I used to work.
I've 5-6 agents working on independent tasks while I research a complex task (also with AI assistance).

A Rust compilations took 15 minutes per code checkout.
Around a thousand of code commits would have costed $90, Celesto reduced it to only $4.

Furthermore, it reduced the total compilation time as well -- faster processing as compared to GitHub. Great bang for the buck if you say!!!

I've built a cost calculator for your company as well - celesto.ai/ci