how to push existing file onto gitlab repository using python

  • Last Update :
  • Techknowledgy :

So there should be a way to update an existing file, instead of creating a new one.

def update_file(self, path, branch, content, message):
   url = "/projects/%s/repository/files" % self.id
url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
   (path, branch, content, message)
r = self.gitlab.rawPut(url)
if r.status_code != 200:
   raise GitlabUpdateError

In May 2016, for the 0.13 version, the file_* methods were deprecated in favor of the files manager.

warnings.warn("`update_file` is deprecated, "
   "use `files.update()` instead",
   DeprecationWarning)

Update a file.
The entire content must be uploaded, as plain text or as base64 encoded text:

f.content = 'new content'
f.save(branch = 'master', commit_message = 'Update testfile')

# or
for binary data
# Note: decode() is required with python 3
for data serialization.You can omit
# it with python 2
f.content = base64.b64encode(open('image.png').read()).decode()
f.save(branch = 'master', commit_message = 'Update testfile', encoding = 'base64')

You can check the differences between a file created on GitLab (and cloned) with your own local file with

git diff--no - index--color--ws - error - highlight = new, old

Suggestion : 2

You can add a file to a repository in your terminal, and then push to GitLab. You can also use the web interface, which may be a simpler solution.,Open a terminal/shell, and change into the folder of your GitLab project. This usually means running the following command until you get to the desired destination:,Create a new branch to add your file into. Submitting changes directly to the default branch should be avoided unless your project is very small and you’re the only person working on it.,Using your standard tool for copying files (for example, Finder in macOS, or File Explorer on Windows), put the file into a directory within the GitLab project.

cd <destination folder>
ls
git status
git add <name of file>
git commit - m "DESCRIBE COMMIT IN A FEW WORDS"
git push origin <branch-name>

Suggestion : 3

I'm currently using python-gitlab package anycodings_gitlab and I think it only supports files.create anycodings_gitlab which creates a new file using supplied anycodings_gitlab string contents. This would result in anycodings_gitlab slightly different file content in my case.,I'm looking for a way to push the file in anycodings_gitlab python onto the repo untouched, can anyone anycodings_gitlab help?,So there should be a way to update an anycodings_gitlab existing file, instead of creating a new anycodings_gitlab one.,is there a way to push existing file onto anycodings_gitlab gitlab project repository in python like the anycodings_gitlab git commit and git push commands, instead of anycodings_gitlab creating a new file?

So there should be a way to update an anycodings_gitlab existing file, instead of creating a new anycodings_gitlab one.

def update_file(self, path, branch, content, message):
   url = "/projects/%s/repository/files" % self.id
url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
   (path, branch, content, message)
r = self.gitlab.rawPut(url)
if r.status_code != 200:
   raise GitlabUpdateError

In May 2016, for the 0.13 version, the anycodings_gitlab file_* methods were deprecated in favor anycodings_gitlab of the files manager.

warnings.warn("`update_file` is deprecated, "
   "use `files.update()` instead",
   DeprecationWarning)

Update a file. The entire content must anycodings_gitlab be uploaded, as plain text or as base64 anycodings_gitlab encoded text:

f.content = 'new content'
f.save(branch = 'master', commit_message = 'Update testfile')

# or
for binary data
# Note: decode() is required with python 3
for data serialization.You can omit
# it with python 2
f.content = base64.b64encode(open('image.png').read()).decode()
f.save(branch = 'master', commit_message = 'Update testfile', encoding = 'base64')

You can check the differences between a anycodings_gitlab file created on GitLab (and cloned) with anycodings_gitlab your own local file with

git diff--no - index--color--ws - error - highlight = new, old

Suggestion : 4

You can export and upload a project to an external URL (see upstream documentation for more details):,Get the content and metadata of a file for a commit, using a blob sha:,You can export projects from gitlab, and re-import them to create new projects or overwrite existing ones.,Upload a file and comment on an issue while using custom markdown to reference the uploaded file:

projects = gl.projects.list()
# List all projects(
   default 20)
projects = gl.projects.list(get_all = True)
# Archived projects
projects = gl.projects.list(archived = 1)
# Limit to projects with a defined visibility
projects = gl.projects.list(visibility = 'public')

# List owned projects
projects = gl.projects.list(owned = True)

# List starred projects
projects = gl.projects.list(starred = True)

# Search projects
projects = gl.projects.list(search = 'keyword')
# Get a project by ID
project_id = 851
project = gl.projects.get(project_id)

# Get a project by name with namespace
project_name_with_namespace = "namespace/project_name"
project = gl.projects.get(project_name_with_namespace)
project = gl.projects.create({
   'name': 'project1'
})
alice = gl.users.list(username = 'alice')[0]
user_project = alice.projects.create({
   'name': 'project'
})
user_projects = alice.projects.list()
# You need to get the id of the group, then use the namespace_id attribute
# to create the group
group_id = gl.groups.list(search = 'my-group')[0].id
project = gl.projects.create({
   'name': 'myrepo',
   'namespace_id': group_id
})

Suggestion : 5

is there a way to push existing file onto gitlab project repository in python like the git commit and git push commands, instead of creating a new file?,What I am looking for is to push an "existing local file" to an empty GitLab project repository,The file after created by python-gitlab misses a space (0x0D) on every line ending. So I guess you're right. However I tried to add core.autocrlf setting or add newline='' in my file open statement or read in binary and decode using different encoding, none of above worked. ,I'm currently using python-gitlab package and I think it only supports files.create which creates a new file using supplied string contents. This would result in slightly different file content in my case.

So there should be a way to update an existing file, instead of creating a new one.

def update_file(self, path, branch, content, message):
   url = "/projects/%s/repository/files" % self.id
url += "?file_path=%s&branch_name=%s&content=%s&commit_message=%s" % \
   (path, branch, content, message)
r = self.gitlab.rawPut(url)
if r.status_code != 200:
   raise GitlabUpdateError

In May 2016, for the 0.13 version, the file_* methods were deprecated in favor of the files manager.

warnings.warn("`update_file` is deprecated, "
   "use `files.update()` instead",
   DeprecationWarning)

Update a file.
The entire content must be uploaded, as plain text or as base64 encoded text:

f.content = 'new content'
f.save(branch = 'master', commit_message = 'Update testfile')

# or
for binary data
# Note: decode() is required with python 3
for data serialization.You can omit
# it with python 2
f.content = base64.b64encode(open('image.png').read()).decode()
f.save(branch = 'master', commit_message = 'Update testfile', encoding = 'base64')

You can check the differences between a file created on GitLab (and cloned) with your own local file with

git diff--no - index--color--ws - error - highlight = new, old