question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’m trying to reset a branch to a particular commit (i.e. git checkout <branch> && git reset [--soft] <commit>) but there doesn’t seem to be a reset command.

@wmhilton do you have plans to implement this in the near future? I don’t want to swamp you with more requests 😅 Seems like an easy thing to do manually by editing stuff in .git/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
juancampacommented, May 22, 2018

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

3reactions
jcubiccommented, May 22, 2018

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}`);
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

git-reset Documentation - Git
You can use git reset to rewind history without changing the contents of your local files, and then successively use git add -p...
Read more >
Git Reset - W3Schools
Git Reset. reset is the command we use when we want to move the repository back to a previous commit , discarding any...
Read more >
How do I use 'git reset --hard HEAD' to revert to a previous ...
First, it's always worth noting that git reset --hard is a potentially dangerous command, since it throws away all your uncommitted changes.
Read more >
Git Reset | The Git Reset Command Explained
The main result of using the git reset command is to repoint the branch label of your currently checked-out branch to a different...
Read more >
A git reset hard example: An easy way to undo local commits ...
Unlike the git cherry-pick and the git revert command, the git reset hard command does not create a new commit but actually rolls...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found