Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you perform a rebase? #932

Open
technologicalMayhem opened this issue Feb 23, 2023 · 2 comments
Open

How do you perform a rebase? #932

technologicalMayhem opened this issue Feb 23, 2023 · 2 comments

Comments

@technologicalMayhem
Copy link

So I am struggling with how you would perform a rebase. I want to handle a case where a remote has changes pushed to it and I now want to rebase the commits that I already made, so I can push them.

I am rather unsure how the rebase function works and how to integrate incoming changes.

Can someone perhaps provide an example?

@extrawurst
Copy link
Contributor

this is just a wrapper around libgit2. for usage help I would suggest reaching out to upstream. everything applies to git2-rs then too

@NYBACHOK
Copy link

NYBACHOK commented Oct 5, 2023

@technologicalMayhem I'm a little bit late. May you show how to push changes?

 self.repository.remote( UPSTREAM_NAME, self.options.upstream_url.0.as_str() )
    .map_err( GitErrors::Git2Error )?
    .fetch( &[ &self.options.default_branch ], Some( &mut fetch_options ), None)
    .map_err( GitErrors::Git2Error )?;
    dbg!( "Add remote and fetch");

    let upstream_master_oid = self.repository.refname_to_id(&format!("refs/remotes/{UPSTREAM_NAME}/{}", self.options.default_branch))
    .map_err( GitErrors::Git2Error )?;
    dbg!(&upstream_master_oid);

    let upstream_master_commit = self.repository.find_annotated_commit(upstream_master_oid)
    .map_err( GitErrors::Git2Error )?;

    let self_master_oid = self.repository.refname_to_id(&format!("refs/remotes/origin/{}", self.options.default_branch))
    .map_err( GitErrors::Git2Error )?;
    dbg!(&self_master_oid);

    let self_master_commit = self.repository.find_annotated_commit(upstream_master_oid)
    .map_err( GitErrors::Git2Error )?;

    let mut rebase_options = git2::RebaseOptions::default();
    rebase_options.inmemory( false );
    
    let mut rebase = self.repository.rebase(
      None,
      Some( &upstream_master_commit ),
      Some(&self_master_commit),
      Some(&mut rebase_options)
    )
    .map_err( GitErrors::Git2Error )?;

    for reb in rebase.by_ref()
    {
      reb.map_err( GitErrors::Git2Error )?;
    }
    
    rebase.finish( None)
    .map_err( GitErrors::Git2Error )?;
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants