Git Product home page Git Product logo

Comments (11)

juancampa avatar juancampa commented on May 14, 2024 4

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.

jcubic avatar jcubic commented on May 14, 2024 3

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.

juancampa avatar juancampa commented on May 14, 2024 2

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.

juancampa avatar juancampa commented on May 14, 2024 1

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.

jcubic avatar jcubic commented on May 14, 2024 1

@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.

jcubic avatar jcubic commented on May 14, 2024

@juancampa do you know how to remove the commit?

What I want is:

git reset --hard HEAD~1

from isomorphic-git.

jcubic avatar jcubic commented on May 14, 2024

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.

billiegoose avatar billiegoose commented on May 14, 2024

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.

billiegoose avatar billiegoose commented on May 14, 2024

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.

davidenke avatar davidenke commented on May 14, 2024

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.

samchungy avatar samchungy commented on May 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.