Comments (11)
Well, IIRC --hard
has two additional effects: clearing the index and updating the working tree (checking out the branch). In theory you could do those two manually with:
const commit = <hash>;
const branch = <branch-name>;
// reset the branch
fs.writeFile(dir + `/.git/refs/heads/${branch}`, commit, (err) => {
// clear the index (if any)
fs.unlink(dir + '/.git/index', (err2) => {
// checkout the branch into the working tree
git.checkout({ dir, fs, ref: branch })
.then(() => {
// Done! (in theory, haven't tested it)
});
})
});
You also need to find out what commit HEAD~1 points to. I'm not sure if you could just pass "HEAD~1"
and have the library resolve it for you, that would be a question for @wmhilton
NOTE: I haven't tested this myself so use carefully
from isomorphic-git.
it works but you made typo in path it should be '/.git/index'
I use this code to reset to HEAD~<NUM>
:
async function gitReset({dir, ref, branch}) {
var re = /^HEAD~([0-9]+)$/
var m = ref.match(re);
if (m) {
var count = +m[1];
var commits = await git.log({fs, dir, depth: count + 1});
var commit = commits.pop().oid;
return new Promise((resolve, reject) => {
fs.writeFile(dir + `/.git/refs/heads/${branch}`, commit, (err) => {
if (err) {
return reject(err);
}
// clear the index (if any)
fs.unlink(dir + '/.git/index', (err) => {
if (err) {
return reject(err);
}
// checkout the branch into the working tree
git.checkout({ dir, fs, ref: branch }).then(resolve);
});
});
});
}
return Promise.reject(`Wrong ref ${ref}`);
}
from isomorphic-git.
If anyone else needs git reset --soft
it's pretty easy to do by manually editing .git/refs/heads
.
This is what I'm currently using:
const commit = <hash>;
const branch = <branch-name>;
fs.writeFile(dir + `/.git/refs/heads/${branch}`, commit, (err) => {
// Done!
});
from isomorphic-git.
I think we're just being lazy. The right thing would be to implement the command, write tests, and post a PR but you know, products gotta ship 🤷♂️
from isomorphic-git.
@davidenke Check my git reset function https://github.com/jcubic/git/blob/gh-pages/js/main.js#L1915 but it was written a while ago, I'm not sure if there are no easier solution.
There is one caveat to reset --hard without arg like in canonical git you need to use ref
equal to HEAD~0
, I'm not sure how it work in canonical git.
from isomorphic-git.
@juancampa do you know how to remove the commit?
What I want is:
git reset --hard HEAD~1
from isomorphic-git.
Will check but this will only work (when it will work) when commit was not pushed, otherwise somehow index should be rewritten and push should be called with force, push force is probably then another feature request.
But for now I'm only interested in not pushed commits.
from isomorphic-git.
This is probably my favorite issue thread right now. Y'all are being ingenious, figuring out how to do these things with the commands currently available. 😁
from isomorphic-git.
I think all the initial issues here were resolved with user-code. One day we will add a build-in command for moving branches around and moving the HEAD
to HEAD~1
etc but I think those can be their own issues.
Plus, git reset
is a terribly named command. I accepted a resetIndex
from @akaJes but reset --soft
does something entirely different - it doesn't touch the index at all. So rather than adding support for the --soft
and --hard
parameters, I will probably make them different functions entirely for better tree-shaking.
from isomorphic-git.
Sorry, but how do you do a conventional git reset --hard
to dump all local changes and commits?
Something with fastCheckout
using force: true
?
It would be really nice to have a cheatsheet or gist for all the "typical" git operations. I switched to isomorphic-git
from nodegit
which wasn't nearly as good as this but in the docs were typical operations with examples...
from isomorphic-git.
Note that the solution by jcubic won't remove files which are staged or added since the commit you are going back to from the workdir which git reset --hard actually does. Instead of the section from fs.unlink onwards I've gone with
// Status Matrix Row Indexes
const FILEPATH = 0;
const HEAD = 1;
const WORKDIR = 2;
const STAGE = 3;
// Status Matrix State
const UNCHANGED = 1;
const allFiles = await git.statusMatrix({ dir, fs });
// Get all files which have been modified or staged - does not include new untracked files or deleted files
const modifiedFiles = allFiles
.filter((row) => row[WORKDIR] > UNCHANGED && row[STAGE] > UNCHANGED)
.map((row) => row[FILEPATH]);
// Delete modified/staged files
await Promise.all(modifiedFiles.map((path) => fs.promises.rm(path)));
await git.checkout({ dir, fs, ref: branch, force: true });
from isomorphic-git.
Related Issues (20)
- Index file is empty (.git/index) HOT 3
- Index file is empty (.git/index) HOT 3
- Metadata API request failed HOT 2
- Catch block causes TypeError in strict mode because it tries to add properties to a string HOT 15
- Metadata API request failed: An internal error caused this command to fail. HOT 3
- Configurable fs HOT 10
- Missing command: `git merge-base` HOT 4
- Get diff / patch list from commit HOT 6
- The `statusMatrix` throw a TypeError when use in the browser with `memfs` HOT 4
- Git.readBlob is too slow. HOT 9
- Properly propagate error encountered when reading response from git-upload-pack POST request HOT 4
- No shallow directory error HOT 3
- isPromiseFs fails on Deno HOT 9
- Improve `normalizePath` performance HOT 6
- pushing a tag to a target branch fail HOT 1
- Empty response from git server when using code bundler HOT 33
- Pushing returning 401 Error: No Anonymous Write Access HOT 6
- Tests are failing on Android HOT 15
- Cloning empty repository fails if git server uses git >= 2.41.0 HOT 5
- Getting a 404 with azure devops HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from isomorphic-git.