Git: Difference between revisions
mNo edit summary |
mNo edit summary |
||
Line 3: | Line 3: | ||
|- | |- | ||
| | | | ||
<pre> | <pre class="bash"> | ||
git config credential.helper store | git config credential.helper store | ||
Line 12: | Line 12: | ||
= Speed-up = | = Speed-up = | ||
<pre> | <pre class="bash"> | ||
# alias 'acp' = add+commit+push | # alias 'acp' = add+commit+push | ||
git config --global alias.acp '!f() { git add . && git commit -m "$@" && git push; }; f' | git config --global alias.acp '!f() { git add . && git commit -m "$@" && git push; }; f' | ||
Line 20: | Line 20: | ||
= Basics = | = Basics = | ||
== Create repository == | == Create repository == | ||
<pre> | <pre class="bash"> | ||
git init # standard | git init # standard | ||
git init --bare # bare without working directory | git init --bare # bare without working directory | ||
Line 26: | Line 26: | ||
== Initial checkout of remote repository == | == Initial checkout of remote repository == | ||
<pre> | <pre class="bash"> | ||
git clone https://gerrit.wikimedia.org/r/p/mediawiki/core.git | git clone https://gerrit.wikimedia.org/r/p/mediawiki/core.git | ||
git clone ssh://user@server/srv/git/git-repo.git | git clone ssh://user@server/srv/git/git-repo.git | ||
Line 33: | Line 33: | ||
== Get latest updates from server == | == Get latest updates from server == | ||
<pre class="bash"> | |||
<pre> | |||
git pull | git pull | ||
(git fetch && git merge) | (git fetch && git merge) | ||
</pre> | </pre> | ||
== Change files and commit == | == Change files and commit == | ||
* show changed files | * show changed files | ||
<pre class="bash"> | |||
<pre> | |||
git diff --cached --name-only | git diff --cached --name-only | ||
git diff --name-status | git diff --name-status | ||
</pre> | </pre> | ||
* add files | * add files | ||
<pre class="bash"> | |||
<pre> | |||
git add * | git add * | ||
</pre> | </pre> | ||
* commit changes | * commit changes | ||
<pre class="bash"> | |||
<pre> | |||
git commit -m "Commit message" | git commit -m "Commit message" | ||
git commit -a # includes add | git commit -a # includes add | ||
git commit -am. | git commit -am. | ||
</pre> | </pre> | ||
* send changes to server | * send changes to server | ||
<pre class="bash"> | |||
<pre> | |||
git push | git push | ||
</pre> | </pre> | ||
== .gitignore files == | == .gitignore files == | ||
<pre> | <pre class="bash"> | ||
*.log | *.log | ||
filename.txt | filename.txt | ||
Line 81: | Line 71: | ||
= Branches = | = Branches = | ||
=== hard reset to specific branch === | === hard reset to specific branch === | ||
<pre> | <pre class="bash"> | ||
git fetch --all | git fetch --all | ||
git reset --hard origin/branchname | git reset --hard origin/branchname | ||
Line 88: | Line 78: | ||
= Repositories = | = Repositories = | ||
=== move repo to new server === | === move repo to new server === | ||
<pre> | <pre class="bash"> | ||
on client: | on client: | ||
git remote set-url origin <URL to my NEW repo location> | git remote set-url origin <URL to my NEW repo location> | ||
Line 95: | Line 85: | ||
=== sparse clone (only specific folder) === | === sparse clone (only specific folder) === | ||
<pre> | <pre class="bash"> | ||
mkdir <repo> | mkdir <repo> | ||
cd <repo> | cd <repo> | ||
Line 110: | Line 100: | ||
=== Store credentials === | === Store credentials === | ||
<pre>git config credential.helper store</pre> | <pre class="bash">git config credential.helper store</pre> | ||
=== Set username and email === | === Set username and email === | ||
<pre> | <pre class="bash"> | ||
git config --global user.email "you@example.com" | git config --global user.email "you@example.com" | ||
git config --global user.name "Your Name" | git config --global user.name "Your Name" | ||
Line 119: | Line 109: | ||
=== Windows - remove old credentials === | === Windows - remove old credentials === | ||
<pre>rundll32.exe keymgr.dll,KRShowKeyMgr</pre> | <pre class="bash">rundll32.exe keymgr.dll,KRShowKeyMgr</pre> | ||
=== post-receive hook === | === post-receive hook === |
Revision as of 17:27, 27 March 2021
Cheet Sheet |
git config credential.helper store git config user.email "email@example.com" git config user.name "username" |
Speed-up
# alias 'acp' = add+commit+push git config --global alias.acp '!f() { git add . && git commit -m "$@" && git push; }; f' git acp "this is my commit comment"
Basics
Create repository
git init # standard git init --bare # bare without working directory
Initial checkout of remote repository
git clone https://gerrit.wikimedia.org/r/p/mediawiki/core.git git clone ssh://user@server/srv/git/git-repo.git
Get latest updates from server
git pull (git fetch && git merge)
Change files and commit
- show changed files
git diff --cached --name-only git diff --name-status
- add files
git add *
- commit changes
git commit -m "Commit message" git commit -a # includes add git commit -am.
- send changes to server
git push
.gitignore files
*.log filename.txt subfolder/*
Branches
hard reset to specific branch
git fetch --all git reset --hard origin/branchname
Repositories
move repo to new server
on client: git remote set-url origin <URL to my NEW repo location> git push -f origin
sparse clone (only specific folder)
mkdir <repo> cd <repo> git init git remote add -f origin <url> git config core.sparseCheckout true echo "some/dir/" >> .git/info/sparse-checkout git pull origin master
Forks
Configuration
Store credentials
git config credential.helper store
Set username and email
git config --global user.email "you@example.com" git config --global user.name "Your Name"
Windows - remove old credentials
rundll32.exe keymgr.dll,KRShowKeyMgr
post-receive hook
- execute script on main git server, after receiving push: hooks/post-receive
#!/bin/bash # unset GIT_INDEX_FILE # not needed for post-receive? git --work-tree=/srv/targetfolder --git-dir=/home/demo/proj/.git checkout -f
- or
#!/bin/bash while read oldrev newrev ref do if [[ $ref =~ .*/master$ ]]; then echo "Master ref received. Deploying master branch to production..." git --work-tree=/var/www/html --git-dir=/home/demo/proj checkout -f else echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server." fi done
- make executable
chmod +x post-receive
Links
- https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps
- https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa
- https://gist.github.com/thomasfr/9691385
Other
Special features
- auto-commit when file changes
inotifywait -q -m -e CLOSE_WRITE --format="git commit -a -m 'autocommit on change' %w" file.txt | sh
- or
while true; do inotifywait -qq -e CLOSE_WRITE ~/.calendar/calendar cd ~/.calendar; git commit -a -m 'autocommit on change' done # inotify "-r" for recursive
- or
- etckeeper or gitwatch