libgit2sharp - How to checkout as "git checkout <commit_hash> ." - libgit2sharp

With git command, to replace the working directory with a commit, we will call:
git checkout <commit_hash> .
How can I implement that with libgit2sharp?

Related

Cannot do a pull request because branch is "identical to main"

I want to do the pull request for my chapter_3.
However, it states that my main and chapter_3 are identical.
How to make my chapter_3 not identical with main? Below i also attached my git reflog
$ git add .
$ git commit -m "chapter 3"
$ git push origin chapter_3
Simply, in your local repository, add a new commit (or now commits) to chapter_3 and push.
Then, since chapter_3 has new commits that main has not, you will be able to initiate a pull request from chapter_3 to main.
The problem is: you were not on local branch chapter_3 when you did your commit. You were on main.
In order to avoid any mishaps, I would:
clone the repository again
create a chapter_3 branch
report your work for chapter 3 there (in the new local clone)
add commit and push
That is:
git clone https://github/com/<me>/<myRepo> newClone
cd newClone
git switch -c chapter_3
# work
git add .
git commit -m "Add chapter 3"
git push -u origin chapter_3
Then you can make your PR.
Notes:
replace <me> by your GitHub account name, and <myRepo> by your target repository name. Don't use < and >: they are placeholder makers.
replace newClone by a new local folder name which does not yet exist (it will be created by the git clone command).

Jenkinsfile to checkout different repo and update a file

I have 2 repos A and B. At the end of build A, I want to update a properties file in repo B with the build number of A
How can I checkout the master branch of repo B in repo A's Jenkinfile just for this stage(which is last) ?
After checkout, can I follow the steps
mentioned below to update the file or is there any better way to
achieve it?
Jenkinsfile of repo A:
stage('Update properties file in repo B') {
steps {
script {
// how do I checkout master branch ofrepo B here?
sh "git config --global user.email jenkins#abc.com"
sh "git config --global user.name Jenkins"
sh(script: 'echo "repoA_VERSION=$BUILD_NUMBER" > version.properties', returnStdout: true).trim()
git add .
git commit -m "Updated version.properties file with ${env.BUILD_NUMBER}"
}
}
}
You just need to call git step:
E.g.
git branch: 'your_branch', credentialsId: 'your_credentials', url: 'your_repo'
If you don't know identifier for your credentials, you can go to:
your_jenkins_server:8080/job/job_name/pipeline-syntax/
where job_name is any job in your server, and you will access to Pipeline Syntax, you can then configure your git checkout and generate command:
For number 2,
the code must be within sh command:
sh """git add .
git commit -m "Updated version.properties file with ${env.BUILD_NUMBER}"
git push
"""
A final recommendation, avoid using git add ., and add files individually or with a wildcard.

In gerrit, how to clone project from HEAD:refs/for/master

I try to use gerrit to do some test.
Use "git push origin HEAD:refs/for/master",
before gerrit code review, I want to do some test by Jenkins.
How to use command to clone the commit from "refs/for/master"?
or fetch to Jenkins job workspace in order to test?
There is only one branch.
"git branch" -> *master
When someone push a commit to review on Gerrit, the commit stays on the "magical" refs/for branch until it's finally submitted (merged). To have the commit locally you need to execute one of the download commands:
Checkout:
git fetch "https://GERRIT-SERVER/a/REPO-PATH" refs/changes/CHANGE-NUMBER && git checkout FETCH_HEAD
Cherry Pick:
git fetch "https://GERRIT-SERVER/a/REPO-PATH" refs/changes/CHANGE-NUMBER && git cherry-pick FETCH_HEAD
Format Patch:
git fetch "https://GERRIT-SERVER/a/REPO-PATH" refs/changes/CHANGE-NUMBER && git format-patch -1 --stdout FETCH_HEAD
Pull:
git pull "https://GERRIT-SERVER/a/REPO-PATH" refs/changes/CHANGE-NUMBER
On Gerrit 3.0, you you'll find these commands at the change screen, clicking on the 3 dots at the up-right and then in the "Download patch" option.
See more info at the Gerrit documentation here.
Using Jenkins, you can execute some of these commands too, but it's easier to use the Gerrit Trigger plugin.

How can I customize / override the "git clone" step in Travis CI?

On the install step, Travis CI clones the repo, which looks similar to this:
git clone --depth=50 --branch=master https://github.com/user/repo.git user/repo
How can I customize / override this?
Background: I am using tag based deploys. The way Travis checks out tagged builds (--branch=<tagname>), the git repository is in a detached state without access to branches. However, for deployment I need to know on which branch I am. My solution is to do a "normal" clone and then switch to the tagged commit.
You can clone the repository again in the install step. That way you clone the repository twice, but it seems to work.
# .travis.yml
install:
- git clone https://github.com/$TRAVIS_REPO_SLUG.git $TRAVIS_REPO_SLUG
- cd $TRAVIS_REPO_SLUG
- git checkout -qf $TRAVIS_COMMIT
Per the Travis docs you can add the following to your .travis.yml to remove the --depth flag:
git:
depth: false
As --depth implies --single-branch, removing this flag means that all branches will be checked out, which isn't the default behaviour.
I found that in order to get access to your whole repo you need the following:
install:
- git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
- git fetch --unshallow --tags
This way you'll have access to remote branches and tags (e.g. can do checkout).
If you're on a tag but no longer want to be in a detached HEAD state you can create a new branch that points to the tag (according to this discussion):
install:
- git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
- git fetch --unshallow --tags
- git symbolic-ref --short HEAD || git checkout -b ${TRAVIS_BRANCH}-test $TRAVIS_BRANCH
Note: git symbolic-ref --short HEAD will fail if you're in a detached HEAD state.
The problem is not really that you are in a detached branch. It is that git does not allow you to fetch the tags: git fetch --tags will only fetch the branch spcified by --branch in the git clone command you gave.
I explain this in more details this answer.
To solve you issue (checking out a specific tag) you can call a script that looks like this, after the repo is cloned:
# Keep track of where Travis put us.
# We are on a detached head, and we need to be able to go back to it.
build_head=$(git rev-parse HEAD)
# fetch the tags
git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --tags
# checkout the tagged commit
git checkout -qf <your tag>
# now do your stuff
# go back to where we were at the beginning
git checkout ${build_head}
Run this during your build to have access to origin tags / branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" 1>/dev/null
git fetch origin -q
After that you can run this command to find branches containing your commit
BRANCHES=`git branch -a --contains "$TRAVIS_TAG"`
I've created a script a loooong time ago to fetch the 'environment' branch where the tag was created for continuous deployment purpose.
It may inspire you: https://gist.github.com/rolebi/a0eb1f783b7f3a5f21a631c8da1582dc
Use it like that:
TARGET_ENV="`test $TRAVIS_TAG && bash scripts/get_branch_for_git_reference.sh $TRAVIS_TAG`"
Disable git clone and then clone the repository again in the install step. The repository will be cloned only once in this way. In this "normal" clone you will able to do what ever you want.
git:
clone: false
install:
- git clone https://github.com/$TRAVIS_REPO_SLUG.git $TRAVIS_REPO_SLUG
- cd $TRAVIS_REPO_SLUG
Or you could just query the remote. Add the following to .travis.yml:
env:
global:
# get all the branches referencing this commit
- REAL_BRANCH=$(git ls-remote origin | sed -n "\|$TRAVIS_COMMIT\s\+refs/heads/|{s///p}")
# or check if we are on a particular branch:
- IS_RELEASE=$(git ls-remote origin | grep "$TRAVIS_COMMIT\s\+refs/heads/release$"
(I am surprised that the git gurus hadn’t come up with this one already)
You can convert the already existing shallow clone to a full clone. To do so execute git fetch --unshallow (available since git version 1.8.3) during the install step.
# .travis.yml
install:
- git fetch --unshallow --tags
The --tags flag forces to fetch all tags even if they don't belong to the checked out branch. This is needed if your build also depends on tags from other branches.

failed to push some refs

Im trying to push to github
I follow the steps:
(before these lines Im located on my app directory)
$ mkdir estaciones
$ cd estaciones
$ git init
>>Initialized empty Git repository in /Users/armandodejesussantoyareales/Documents/project_newbie/Estaciones/estaciones/.git/
$ touch README
$ git add README
$ git commit -m "phase 3 estaciones"
>>[master (root-commit) 4462be3] phase 3 estaciones
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100755 README
(I don't know if that message is an error)
$ git remote add origin git#github.com:asantoya/estaciones.git
$ git push -u origin master
but always I have the same problem,late that I type that i got the next error
To git#github.com:asantoya/estaciones.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'git#github.com:asantoya/estaciones.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
This means that the remote repository already has a master branch and it has stuff in that branch that has been added since you made commits to your local copy. This is referring to the code visible at https://github.com/asantoya/estaciones
Start by cloning the repo from github, then adding your changes to it, then pushing:
git clone https://github.com/asantoya/estaciones.git
git checkout -b nameofyourbranch master
Add some files etc.
git commit -a -m "Your commit message"
git push origin nameofyourbranch

Resources