git push failing with Access denied to Gitlab - gitlab

I am using git to push my code to Gitlab. Following are the commands I am using
git clone https://<mygitlab.com>/gke-app-namespace-deploy.git
cd gke-app-namespace-deploy
git checkout -b master
git branch
main
* master
git commit -m "first commit"
git remote set-url origin https://<mygitlab.com>/askar/gke-app-ns-deploy.git
$ git push -u origin master
Username for 'https://<mygitlab.com>': askar
Password for 'https://askar':
remote: HTTP Basic: Access denied
fatal: Authentication failed for 'https://<mygitlab.com>/askar/gke-app-ns-deploy.git/'
The same credentials works while downloading the code but I am getting Access Denied message while uploading the code.
What I am trying to do is clone code from gke-app-namespace-deploy.git' and push to a new repository 'gke-app-ns-deploy.git. Not sure if git by design will allow this.

Try,
git remote add origin https://<access-token-name>:<access-token>#gitlab.com/myuser/myrepo.git
From: https://stackoverflow.com/a/52074198/2675670

Related

How to add the 'DefaultCollection', when cloning a repo in yaml

I have this YAML snippet, to clone a second repo in my pipeline.
resources:
repositories:
- repository: clone-scripts
type: git
name: 'DevOpsProject/pipeline-scripts'
ref: 'refs/heads/main'
however, when executed, it shows this error
git remote add origin https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts
git config gc.auto 0
git config --get-all http.https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts.extraheader
git config --get-all http.proxy
git config http.version HTTP/1.1
git -c http.extraheader="AUTHORIZATION: bearer ***" fetch --force --tags --prune --progress --no-recurse-submodules origin
remote: TF401019: The Git repository with name or identifier pipeline-scripts does not exist or you do not have permissions for the operation you are attempting.
fatal: repository 'https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts/' not found
what I have seen, is that it is not taking into account the 'DefaultCollection' when executing
git remote add origin https://dev.visualstudio.com/DefaultCollection/DevOpsProject/_git/pipeline-scripts (OK)
But how do you execute that, it's wrong
git remote add origin https://dev.visualstudio.com/DevOpsProject/_git/pipeline-scripts
Any ideas? in the Azure documentation, I have read but i can't find any information about that.
resources:
repositories:
- repository: clone-scripts
type: git
name: 'DefaultCollection/DevOpsProject/pipeline-scripts'
ref: 'refs/heads/main'
Its just to check those permisions in repo config

Commit has bugs

I committed a branch and it is merged with master. Later on production, it is found to be bug in the commit.
I need to raise a PR with previous commit of master against master.
I tried the following
git checkout <specific-commit-id>
git checkout -b <new-branch-name>
git push origin <new-branch-name>
When I try to raise PR against master, it says
The source branch is 2 commits behind the target branch
Expected it to allow me to merge it in master but it didn't allow
How can I do so?
Follow the steps below,
git checkout <branch-name>
git checkout -b <new-branch-name> <properly-working-commit-id-of-current-branch-which-we-want-to-deploy>
git add .
git commit --amend -m 'commit-name'
git push origin <branch-name>
By doing so it allows us to merge in GitLab

Error while pushing from bitbucket repo to heroku. It says my repo is a shallow clone

I created a pipeline to deploy my code from Bitbucket repo to Heroku server.
my bitbucket-pipelines.yml
image: node:10.15.3
pipelines:
default:
- step:
script: # Modify the commands below to build your repository.
- npm install
- git push https://heroku:<my heroku api key>#git.heroku.com/<my heroku app's name>.git HEAD
After pushing my code to my bitbucket repo, the pipeline runs, but fails while pushing the code to heroku git.
Below is the error:
Push rejected, source repository is a shallow clone. Unshallow it with `git fetch --all --unshallow` and try pushing again.
But then by bit bucket repo is not shallow. The command git rev-parse --is-shallow-repository returns false.
You can use the following git command before the push
git filter-branch -- --all
So it would be in your script:
...
- git filter-branch -- --all
- git push https://heroku:<my heroku api key>#git.heroku.com/<my heroku app's name>.git HEAD

Unable to push into gitlab as part of CI pipeline

I am trying to modify the package.json for version string and committing as part of CI but git push is failing for me. Following is the code in gitlab-ci.yml in my stage:
before_script:
- git remote set-url origin https://userid:${CI_PUSH_TOKEN}#gitlaburl.com/datastorage/project.git
script:
- call npm install gulp-json-modify --save
- git checkout -B myBranch
- call npm run modifyBuildVersion
- git add package.json
- git commit -m "[skip_ci]Automated commit from CI runner"
- git push --follow-tags origin myBranch
Where CI_PUSH_TOKEN is an environment variable I created under settings->CI/CD and this key contains the value of personal access tokens.
In the step of git push I am getting an error
"remote: HTTP Basic: Access denied fatal: Authentication failed for"
Any idea what's going wrong here?
I tried to search around but didn't get much help.
Did you give at least developer right for userid on your repo ?
If you use use the gitlab-ci-token as user, you are not able to push for the moment (in Gitlab 11.8). There is an open feature request.

Jenkins2 - Getting error while trying to execute a Git command using 'Execute Shell'

I'm using Jenkins 2 and trying to execute below GIT command using 'Execute Shell' in Build section.
git config --global user.email $GITHUB_LOGIN
git config --global user.name $GITHUB_LOGIN
git tag -a $BUILD_NUMBER -m "Version $BUILD_NUMBER"
git push origin --tags
With this, I'm getting below error when i run the job
git push origin --tags
fatal: could not read Username for 'https://github.com': Device not configured
In the build logs, And I've observed below statement and per my understanding, I believe Jenkins is using GIT_ASKPASS to pull the changes from GitHub.
using GIT_ASKPASS to set credentials GitHubCredentials
With that said, Can someone help me understand if I can use 'GIT_ASKPASS' to fix this issue? If yes, How can i use it in my situation?
I got this resolved with the below code snippet
git config --global user.name <git_username>
git push https://<git_username>:<git_password>#<git_repo_url> --tags

Resources