File management
Git provides file management capabilities that help you to track changes, collaborate with others, and manage large files efficiently.
File history
Use git log
to view a file’s complete history and understand how it has changed over time.
The file history shows you:
- The author of each change.
- The date and time of each modification.
- The specific changes made in each commit.
For example, to view history
information about the CONTRIBUTING.md
file in the root
of the gitlab
repository, run:
git log CONTRIBUTING.md
Example output:
commit b350bf041666964c27834885e4590d90ad0bfe90
Author: Nick Malcolm <nmalcolm@gitlab.com>
Date: Fri Dec 8 13:43:07 2023 +1300
Update security contact and vulnerability disclosure info
commit 8e4c7f26317ff4689610bf9d031b4931aef54086
Author: Brett Walker <bwalker@gitlab.com>
Date: Fri Oct 20 17:53:25 2023 +0000
Fix link to Code of Conduct
and condense some of the verbiage
Check previous changes to a file
Use git blame
to see who made the last change to a file and when.
This helps to understand the context of a file’s content,
resolve conflicts, and identify the person responsible for a specific change.
If you want to find blame
information about a README.md
file in the local directory:
- Open a terminal or command prompt.
- Go to your Git repository.
-
Run the following command:
git blame README.md
- To navigate the results page, press Space.
- To exit out of the results, press Q.
This output displays the file content with annotations showing the commit SHA, author, and date for each line. For example:
58233c4f1054c (Dan Rhodes 2022-05-13 07:02:20 +0000 1) ## Contributor License Agreement
b87768f435185 (Jamie Hurewitz 2017-10-31 18:09:23 +0000 2)
8e4c7f26317ff (Brett Walker 2023-10-20 17:53:25 +0000 3) Contributions to this repository are subject to the
58233c4f1054c (Dan Rhodes 2022-05-13 07:02:20 +0000 4)
Git LFS
Git Large File Storage (LFS) is an extension that helps you manage large files in Git repositories. It replaces large files with text pointers in Git, and stores the file contents on a remote server.
Prerequisites:
- Download and install the appropriate version of the CLI extension for Git LFS for your operating system.
- Configure your project to use Git LFS.
- Install the Git LFS pre-push hook. To do this, run
git lfs install
in the root directory of your repository.
Add and track files
To add a large file into your Git repository and track it with Git LFS:
-
Configure tracking for all files of a certain type. Replace
iso
with your desired file type:git lfs track "*.iso"
This command creates a
.gitattributes
file with instructions to handle all ISO files with Git LFS. The following line is added to your.gitattributes
file:*.iso filter=lfs -text
- Add a file of that type,
.iso
, to your repository. -
Track the changes to both the
.gitattributes
file and the.iso
file:git add .
-
Ensure you’ve added both files:
git status
The
.gitattributes
file must be included in your commit. It if isn’t included, Git does not track the ISO file with Git LFS.Ensure the files you’re changing are not listed in a.gitignore
file. If they are, Git commits the change locally but doesn’t push it to your upstream repository. -
Commit both files to your local copy of the repository:
git commit -m "Add an ISO file and .gitattributes"
-
Push your changes upstream. Replace
main
with the name of your branch:git push origin main
- Create a merge request.
git lfs migrate
to convert existing files to use Git LFS.Stop tracking a file
When you stop tracking a file with Git LFS, the file remains on disk because it’s still part of your repository’s history.
To stop tracking a file with Git LFS:
-
Run the
git lfs untrack
command and provide the path to the file:git lfs untrack doc/example.iso
-
Use the
touch
command to convert it back to a standard file:touch doc/example.iso
-
Track the changes to the file:
git add .
- Commit and push your changes.
- Create a merge request and request a review.
- Merge the request into the target branch.
git lfs untrack
,
the object shows as modified
in git status
.Stop tracking all files of a single type
To stop tracking all files of a particular type in Git LFS:
-
Run the
git lfs untrack
command and provide the file type to stop tracking:git lfs untrack "*.iso"
-
Use the
touch
command to convert the files back to standard files:touch *.iso
-
Track the changes to the files:
git add .
- Commit and push your changes.
- Create a merge request and request a review.
- Merge the request into the target branch.
File locks
File locks help prevent conflicts and ensure that only one person can edit a file at a time. It’s a good option for:
- Binary files that can’t be merged. For example, design files and videos.
- Files that require exclusive access during editing.
Prerequisites:
- You must have Git LFS installed.
- You must have the Maintainer role for the project.
Configure file locks
To configure file locks for a specific file type:
-
Use the
git lfs track
command with the--lockable
option. For example, to configure PNG files:git lfs track "*.png" --lockable
This command creates or updates your
.gitattributes
file with the following content:*.png filter=lfs diff=lfs merge=lfs -text lockable
-
Push the
.gitattributes
file to the remote repository for the changes to take effect.After a file type is registered as lockable, it is automatically marked as read-only.
Configure file locks without LFS
To register a file type as lockable without using Git LFS:
-
Edit the
.gitattributes
file manually:*.pdf lockable
-
Push the
.gitattributes
file to the remote repository.
Lock and unlock files
To lock or unlock a file with exclusive file locking:
- Open a terminal window in your repository directory.
-
Run one of the following commands:
Lock a filegit lfs lock path/to/file.png
Unlock a filegit lfs unlock path/to/file.png
Unlock a file by IDgit lfs unlock --id=123
Force unlock a filegit lfs unlock --id=123 --force
View locked files
To view locked files:
- Open a terminal window in your repository.
-
Run the following command:
git lfs locks
The output lists the locked files, the users who locked them, and the file IDs.
In the GitLab UI:
- The repository file tree displays an LFS badge for files tracked by Git LFS.
- Exclusively-locked files show a padlock icon. LFS-Locked files
You can also view and remove existing locks from the GitLab UI.
Lock and edit a file
To lock a file, edit it, and optionally unlock it:
-
Lock the file:
git lfs lock <file_path>
- Edit the file.
-
Optional. Unlock the file when you’re done:
git lfs unlock <file_path>