Git: Difference between revisions
No edit summary |
|||
(27 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Git is | Git is a popular version control system made by Linus Torvalds. | ||
== | ==Installation== | ||
===Basic Usage | ===Windows=== | ||
Download [https://git-scm.com/downloads Git for Windows] or use [https://community.chocolatey.org/packages/git chocolatey]. | |||
<pre> | |||
choco install git --params "/WindowsTerminalProfile" | |||
</pre> | |||
===Linux=== | |||
[https://git-scm.com/download/linux git-scm install] | |||
<syntaxhighlight lang="bash"> | |||
# PPA for the latest stable version. | |||
sudo add-apt-repository ppa:git-core/ppa | |||
sudo apt update | |||
sudo apt install git git-lfs | |||
</syntaxhighlight> | |||
==Basic Usage== | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# Clone or download a repository | # Clone or download a repository | ||
git clone <url> | git clone <url> [<foldername>] | ||
# cd into the cloned folder | |||
cd <foldername> | |||
# Note that some repos have submodules which will also need to be cloned | |||
git submodule update --init --recursive | |||
# Stage your changes | # Stage your changes | ||
git add <my files> | git add <my files> | ||
# | # Or stage all files | ||
git add . | # git add . | ||
# Make a commit | # Make a commit | ||
Line 23: | Line 42: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Branches=== | ==Branches== | ||
Branches allow you to work on the repository without creating new forks and using more disk space. | |||
You can use them to create and test changes from a static copy of the code repo. | |||
You can also discard a branch if you decide the feature is no longer needed. | |||
Once the feature has been fully developed, you should rebase the branch onto the head of the main branch. | |||
Then you can merge the branch to add the feature to main/master. | |||
<syntaxhighlight lang="bash"> | |||
# Make a branch | |||
git checkout -b my_feature | |||
# Add and commit to this branch | |||
git add . | |||
git commit -m "My commit message" | |||
# Push | |||
git push --set-upstream origin my_feature | |||
# Checkout a branch | |||
git checkout my_branch | |||
# Checkout a single file from another branch | |||
git checkout my_branch -- other_file.txt | |||
</syntaxhighlight> | |||
==Patches== | |||
Patches are a way to represent and share changes without actually making commits or new branches. | |||
<syntaxhighlight lang="bash"> | |||
# For unstaged changes | |||
git diff > changes.patch | |||
# For staged changes | |||
git diff --cached > changes.patch | |||
# You can also add --binary to include binary files | |||
# Apply the patch | |||
git apply changes.patch | |||
</syntaxhighlight> | |||
See [https://stackoverflow.com/questions/5159185/create-a-git-patch-from-the-uncommitted-changes-in-the-current-working-directory create git patch] | |||
==Pull Requests== | |||
See [https://stackoverflow.com/questions/20289599/are-pull-requests-part-of-git-or-a-feature-of-tools-like-github-gerrit-and-atl Are pull requests are part of Git?] | |||
Pull requests are a way to submit changes to repositories you do not manage. | |||
First, you fork the repository to create a working-copy for yourself to develop on. | |||
Once your changes are finished, you create a pull request asking the original repo to incorporate your changes. | |||
It's easiest to just use the web interfaces of GitHub or GitLab for this. | |||
To do this manually, see [https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project Git Book: Contributing to a project]. | |||
==Migrating Repositories== | |||
How to migrate repositories to another Git server | How to migrate repositories to another Git server | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 39: | Line 102: | ||
git push --all <new_repo> | git push --all <new_repo> | ||
</syntaxhighlight> | |||
==Changing Remote URL== | |||
[https://help.github.com/en/articles/changing-a-remotes-url Reference]<br> | |||
You will need to change the remote url if it is changed on the server. E.g. if you change the project name.<br> | |||
<syntaxhighlight lang="bash"> | |||
git remote set-url origin <new-url> | |||
</syntaxhighlight> | |||
==Special Files== | |||
===<code>.gitignore</code>=== | |||
<code>.gitignore</code> is used to ignore certain files to make sure they do not get accidentally pushed to the remote git repo.<br> | |||
You can find a collection of .gitignore files on [https://github.com/github/gitignore Github's gitignore repo].<br> | |||
Or you can create a custom .gitignore file at [https://www.gitignore.io gitignore.io]. | |||
===<code>.gitattributes</code>=== | |||
<code>.gitattributes</code> are used to give attributes to paths.<br> | |||
They can be used to assign programming languages to filenames.<br> | |||
They are also used to identify which files should be stored in the lfs.<br> | |||
[https://gitlab.davidl.me/-/snippets/1/raw/master/lfs.gitattributes Here] is an example to store video and images in the lfs.<br> | |||
==Clean== | |||
[https://www.atlassian.com/git/tutorials/undoing-changes/git-clean Atlassian Reference]<br> | |||
Use <code>git clean</code> to delete untracked files.<br> | |||
Add <code>-x</code> to also delete gitignore'd files. | |||
<syntaxhighlight lang="bash"> | |||
# Force delete any untracked files and directories | |||
git clean -fd | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Git Large File Storage (LFS)== | ==Git Large File Storage (LFS)== | ||
[https://github.com/git-lfs/git-lfs/wiki/Tutorial Reference] | [https://github.com/git-lfs/git-lfs/wiki/Tutorial Reference] | ||
* Note that Github charges for LFS so it's best to keep binary files outside of GitHub. | |||
==Sparse checkout== | |||
If you're interacting with a monorepo like [https://github.com/google-research/google-research google-research/google-research], you may be interested in git sparse checkout.<br> | |||
See [https://git-scm.com/docs/git-sparse-checkout Git - git-sparse-checkout Documentation] and [https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/ Bring your monorepo down to size with sparse-checkout | The GitHub Blog].<br> | |||
This is a relatively new feature so be sure to use an updated version of git. | |||
<syntaxhighlight lang="bash"> | |||
git clone --no-checkout $url | |||
cd $project | |||
git sparse-checkout init | |||
git sparse-checkout add $folder | |||
</syntaxhighlight> |
Latest revision as of 13:14, 31 May 2023
Git is a popular version control system made by Linus Torvalds.
Installation
Windows
Download Git for Windows or use chocolatey.
choco install git --params "/WindowsTerminalProfile"
Linux
# PPA for the latest stable version.
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git git-lfs
Basic Usage
# Clone or download a repository
git clone <url> [<foldername>]
# cd into the cloned folder
cd <foldername>
# Note that some repos have submodules which will also need to be cloned
git submodule update --init --recursive
# Stage your changes
git add <my files>
# Or stage all files
# git add .
# Make a commit
git commit -m "My commit message"
# Push your changes to the repository
git push
# Pull the latest changes from the repository
# Equivalent to git fetch && git merge FETCH_HEAD
git pull
Branches
Branches allow you to work on the repository without creating new forks and using more disk space.
You can use them to create and test changes from a static copy of the code repo.
You can also discard a branch if you decide the feature is no longer needed.
Once the feature has been fully developed, you should rebase the branch onto the head of the main branch.
Then you can merge the branch to add the feature to main/master.
# Make a branch
git checkout -b my_feature
# Add and commit to this branch
git add .
git commit -m "My commit message"
# Push
git push --set-upstream origin my_feature
# Checkout a branch
git checkout my_branch
# Checkout a single file from another branch
git checkout my_branch -- other_file.txt
Patches
Patches are a way to represent and share changes without actually making commits or new branches.
# For unstaged changes
git diff > changes.patch
# For staged changes
git diff --cached > changes.patch
# You can also add --binary to include binary files
# Apply the patch
git apply changes.patch
See create git patch
Pull Requests
See Are pull requests are part of Git?
Pull requests are a way to submit changes to repositories you do not manage.
First, you fork the repository to create a working-copy for yourself to develop on.
Once your changes are finished, you create a pull request asking the original repo to incorporate your changes.
It's easiest to just use the web interfaces of GitHub or GitLab for this.
To do this manually, see Git Book: Contributing to a project.
Migrating Repositories
How to migrate repositories to another Git server
git clone --mirror <original_repo>
cd <repo>
# Run the following lines only if you're using lfs
git lfs fetch --all
git lfs push --all <new_repo>
git push --all <new_repo>
Changing Remote URL
Reference
You will need to change the remote url if it is changed on the server. E.g. if you change the project name.
git remote set-url origin <new-url>
Special Files
.gitignore
.gitignore
is used to ignore certain files to make sure they do not get accidentally pushed to the remote git repo.
You can find a collection of .gitignore files on Github's gitignore repo.
Or you can create a custom .gitignore file at gitignore.io.
.gitattributes
.gitattributes
are used to give attributes to paths.
They can be used to assign programming languages to filenames.
They are also used to identify which files should be stored in the lfs.
Here is an example to store video and images in the lfs.
Clean
Atlassian Reference
Use git clean
to delete untracked files.
Add -x
to also delete gitignore'd files.
# Force delete any untracked files and directories
git clean -fd
Git Large File Storage (LFS)
- Note that Github charges for LFS so it's best to keep binary files outside of GitHub.
Sparse checkout
If you're interacting with a monorepo like google-research/google-research, you may be interested in git sparse checkout.
See Git - git-sparse-checkout Documentation and Bring your monorepo down to size with sparse-checkout | The GitHub Blog.
This is a relatively new feature so be sure to use an updated version of git.
git clone --no-checkout $url
cd $project
git sparse-checkout init
git sparse-checkout add $folder