debian-mirror-gitlab/doc/user/project/file_lock.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

230 lines
7.4 KiB
Markdown
Raw Normal View History

2020-10-24 23:57:45 +05:30
---
stage: Create
group: Source Code
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2020-10-24 23:57:45 +05:30
---
2021-03-11 19:13:27 +05:30
# File Locking **(FREE)**
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
Preventing wasted work caused by unresolvable merge conflicts requires
a different way of working. This means explicitly requesting write permissions,
and verifying no one else is editing the same file before you start.
2019-07-31 22:56:46 +05:30
2023-04-23 21:23:45 +05:30
Although branching strategies typically work well enough for source code and
2020-11-24 15:15:51 +05:30
plain text because different versions can be merged together, they do not work
for binary files.
2020-05-24 23:13:21 +05:30
2021-06-08 01:23:25 +05:30
When file locking is setup, lockable files are **read-only** by default.
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
When a file is locked, only the user who locked the file may modify it. This
user is said to "hold the lock" or have "taken the lock", since only one user
can lock a file at a time. When a file or directory is unlocked, the user is
said to have "released the lock".
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
GitLab supports two different modes of file locking:
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
- [Exclusive file locks](#exclusive-file-locks) for binary files: done **through
the command line** with Git LFS and `.gitattributes`, it prevents locked
2022-05-07 20:08:51 +05:30
files from being modified on any branch.
2020-11-24 15:15:51 +05:30
- [Default branch locks](#default-branch-file-and-directory-locks): done
**through the GitLab UI**, it prevents locked files and directories being
2022-05-07 20:08:51 +05:30
modified on the default branch.
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
## Permissions
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
Locks can be created by any person who has at least
2022-04-04 11:22:00 +05:30
Developer role in the repository.
2019-07-31 22:56:46 +05:30
2021-04-17 20:07:23 +05:30
Only the user who locked the file or directory can edit locked files. Other
users are prevented from modifying locked files by pushing, merging,
or any other means, and are shown an error like: `The path '.gitignore' is
2020-11-24 15:15:51 +05:30
locked by Administrator`.
2019-07-31 22:56:46 +05:30
2022-05-07 20:08:51 +05:30
## Exclusive file locks
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
This process allows you to lock single files or file extensions and it is
done through the command line. It doesn't require GitLab paid subscriptions.
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
Git LFS is well known for tracking files to reduce the storage of
2021-11-18 22:05:49 +05:30
Git repositories, but it can also be used for [locking files](https://github.com/git-lfs/git-lfs/wiki/File-Locking).
2020-11-24 15:15:51 +05:30
This is the method used for Exclusive File Locks.
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
### Install Git LFS
Before getting started, make sure you have [Git LFS installed](../../topics/git/lfs/index.md) in your computer. Open a terminal window and run:
```shell
git-lfs --version
```
2021-04-17 20:07:23 +05:30
If it doesn't recognize this command, you must install it. There are
2023-06-20 00:43:36 +05:30
several [installation methods](https://git-lfs.com/) that you can
2020-11-24 15:15:51 +05:30
choose according to your OS. To install it with Homebrew:
```shell
brew install git-lfs
```
Once installed, **open your local repository in a terminal window** and
2021-01-03 14:25:43 +05:30
install Git LFS in your repository. If you're sure that LFS is already installed,
2021-04-17 20:07:23 +05:30
you can skip this step. If you're unsure, re-installing it does no harm:
2020-11-24 15:15:51 +05:30
```shell
git lfs install
```
2023-04-23 21:23:45 +05:30
For more information, see [Using Git LFS](../../topics/git/lfs/index.md#using-git-lfs).
2020-11-24 15:15:51 +05:30
### Configure Exclusive File Locks
2019-09-30 21:07:59 +05:30
2022-04-04 11:22:00 +05:30
You need the Maintainer role
2020-11-24 15:15:51 +05:30
Exclusive File Locks for your project through the command line.
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
The first thing to do before using File Locking is to tell Git LFS which
2021-04-17 20:07:23 +05:30
kind of files are lockable. The following command stores PNG files
2020-11-24 15:15:51 +05:30
in LFS and flag them as lockable:
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
```shell
git lfs track "*.png" --lockable
```
2019-09-30 21:07:59 +05:30
2021-04-17 20:07:23 +05:30
After executing the above command a file named `.gitattributes` is
2020-11-24 15:15:51 +05:30
created or updated with the following content:
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
```shell
*.png filter=lfs diff=lfs merge=lfs -text lockable
```
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
You can also register a file type as lockable without using LFS (to be able, for example,
to lock/unlock a file you need in a remote server that
implements the LFS File Locking API). To do that you can edit the
`.gitattributes` file manually:
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
```shell
*.pdf lockable
```
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
The `.gitattributes` file is key to the process and **must**
be pushed to the remote repository for the changes to take effect.
2019-09-30 21:07:59 +05:30
2021-04-17 20:07:23 +05:30
After a file type has been registered as lockable, Git LFS makes
them read-only on the file system automatically. This means you
must **lock the file** before [editing it](#edit-lockable-files).
2019-09-30 21:07:59 +05:30
2020-11-24 15:15:51 +05:30
### Lock files
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
By locking a file, you verify that no one else is editing it, and
2021-04-29 21:17:54 +05:30
prevent anyone else from editing the file until you're done. On the other
2020-11-24 15:15:51 +05:30
hand, when you unlock a file, you communicate that you've finished editing
and allow other people to edit it.
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
To lock or unlock a file with Exclusive File Locking, open a terminal window
in your repository directory and run the commands as described below.
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
To **lock** a file:
2019-07-31 22:56:46 +05:30
2020-03-13 15:44:24 +05:30
```shell
2020-11-24 15:15:51 +05:30
git lfs lock path/to/file.png
2019-07-31 22:56:46 +05:30
```
2020-11-24 15:15:51 +05:30
To **unlock** a file:
```shell
git lfs unlock path/to/file.png
```
You can also unlock by file ID (given by LFS when you [view locked files](#view-exclusively-locked-files)):
```shell
git lfs unlock --id=123
```
If for some reason you need to unlock a file that was not locked by
yourself, you can use the `--force` flag as long as you have **Maintainer**
permissions to the project:
```shell
git lfs unlock --id=123 --force
```
You can normally push files to GitLab whether they're locked or unlocked.
2021-02-22 17:27:13 +05:30
NOTE:
2020-11-24 15:15:51 +05:30
Although multi-branch file locks can be created and managed through the Git LFS
command line interface, file locks can be created for any file.
### View exclusively-locked files
To list all the files locked with LFS locally, open a terminal window in your
2021-01-03 14:25:43 +05:30
repository and run:
2020-11-24 15:15:51 +05:30
```shell
git lfs locks
```
The output lists the locked files followed by the user who locked each of them
and the files' IDs.
2021-04-17 20:07:23 +05:30
On the repository file tree, GitLab displays an LFS badge for files
2020-11-24 15:15:51 +05:30
tracked by Git LFS plus a padlock icon on exclusively-locked files:
![LFS-Locked files](img/lfs_locked_files_v13_2.png)
You can also [view and remove existing locks](#view-and-remove-existing-locks) from the GitLab UI.
2021-02-22 17:27:13 +05:30
NOTE:
2021-04-17 20:07:23 +05:30
When you rename an exclusively-locked file, the lock is lost. You must
2020-11-24 15:15:51 +05:30
lock it again to keep it locked.
### Edit lockable files
Once the file is [configured as lockable](#configure-exclusive-file-locks), it is set to read-only.
Therefore, you need to lock it before editing it.
Suggested workflow for shared projects:
1. Lock the file.
1. Edit the file.
1. Commit your changes.
2021-01-03 14:25:43 +05:30
1. Push to the repository.
2020-11-24 15:15:51 +05:30
1. Get your changes reviewed, approved, and merged.
1. Unlock the file.
## Default branch file and directory locks **(PREMIUM)**
2022-05-07 20:08:51 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/440) in GitLab 8.9.
2020-11-24 15:15:51 +05:30
This process allows you to lock one file at a time through the GitLab UI and
2021-03-11 19:13:27 +05:30
requires access to [GitLab Premium](https://about.gitlab.com/pricing/)
or higher tiers.
2020-11-24 15:15:51 +05:30
2021-09-04 01:27:46 +05:30
Default branch file and directory locks only apply to the
[default branch](repository/branches/default.md) set in the project's settings.
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
Changes to locked files on the default branch are blocked, including merge
2020-11-24 15:15:51 +05:30
requests that modify locked files. Unlock the file to allow changes.
### Lock a file or a directory
To lock a file:
1. Open the file or directory in GitLab.
2023-05-27 22:25:52 +05:30
1. In the upper-right corner, above the file, select **Lock**.
2021-12-11 22:18:48 +05:30
1. On the confirmation dialog box, select **OK**.
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
If you do not have permission to lock the file, the button is not enabled.
2020-11-24 15:15:51 +05:30
2021-12-11 22:18:48 +05:30
To view the user who locked the file (if it was not you), hover over the button.
2020-11-24 15:15:51 +05:30
### View and remove existing locks
2021-12-11 22:18:48 +05:30
To view and remove file locks:
2022-10-11 01:57:18 +05:30
1. On the top bar, select **Main menu > Projects** and find your project.
2023-05-27 22:25:52 +05:30
1. On the left sidebar, select **Repository > Locked files**.
2019-07-31 22:56:46 +05:30
2020-11-24 15:15:51 +05:30
This list shows all the files locked either through LFS or GitLab UI.
2021-12-11 22:18:48 +05:30
Locks can be removed by their author, or any user
2022-04-04 11:22:00 +05:30
with at least the Maintainer role.