Git: Difference between revisions

 
(12 intermediate revisions by the same user not shown)
Line 3: Line 3:
==Installation==
==Installation==
===Windows===
===Windows===
Download [https://git-scm.com/downloads Git for 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===
===Linux===
[https://git-scm.com/download/linux git-scm install]
<syntaxhighlight lang="bash">
<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
sudo apt install git git-lfs
</syntaxhighlight>
</syntaxhighlight>
Line 35: Line 43:


==Branches==
==Branches==
You should make branches per-feature. 
Branches allow you to work on the repository without creating new forks and using more disk space.
This allows you to work on your feature from a static copy of the code repo.   
You can use them to create and test changes from a static copy of the code repo.   
You can also discard this branch if you decide the feature is no longer needed.   
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 master.   
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 master.
Then you can merge the branch to add the feature to main/master.


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
Line 58: Line 66:
git checkout my_branch -- other_file.txt
git checkout my_branch -- other_file.txt
</syntaxhighlight>
</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==
==Pull Requests==
Line 89: Line 111:
</syntaxhighlight>
</syntaxhighlight>


==<code>.gitignore</code>==
==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>
<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>
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].
Or you can create a custom .gitignore file at [https://www.gitignore.io gitignore.io].


==<code>.gitattributes</code>==
===<code>.gitattributes</code>===
.gitattributes are used to give attributes to paths.<br>
<code>.gitattributes</code> are used to give attributes to paths.<br>
They can be used to assign programming languages to filenames.<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>
They are also used to identify which files should be stored in the lfs.<br>
Line 111: Line 134:
==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>