Git: Difference between revisions

From Wiki
mNo edit summary
mNo edit summary
Line 10: Line 10:
</pre>
</pre>
|}
|}


= Speed-up =
= Speed-up =
Line 21: Line 19:


= Basics =
= Basics =
== Create repository (on server) ==
== Create repository ==
<blockquote>
<pre>
<pre>
git init
git init             # standard
git init --bare
git init --bare     # bare without working directory
</pre>
</pre>
</blockquote>


== Initial checkout of remote repository ==
== Initial checkout of remote repository ==
<blockquote>
<pre>
<pre>
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
</pre>
</pre>
</blockquote>
 


== Get latest updates from server ==
== Get latest updates from server ==
Line 64: Line 59:
<blockquote>
<blockquote>
<pre>
<pre>
git commit -m "Commit-Nachricht"
git commit -m "Commit message"
git commit -a                      # includes add
git commit -a                      # includes add
git commit -am.
git commit -am.
Line 77: Line 72:
</blockquote>
</blockquote>


== Set username and email ==
== .gitignore files ==
<blockquote>
<pre>
<pre>
git config --global user.email "you@example.com"
*.log
git config --global user.name "Your Name"
filename.txt
subfolder/*
</pre>
</pre>
</blockquote>


= Branches =
=== hard reset to specific branch ===
<pre>
git fetch --all
git reset --hard origin/branchname
</pre>


== move repo to new server ==  
= Repositories =
=== move repo to new server ===
<pre>
<pre>
on client:
on client:
Line 93: Line 94:
</pre>
</pre>


== sparse clone (only specific folder) ==
=== sparse clone (only specific folder) ===
<pre>
<pre>
mkdir <repo>
mkdir <repo>
Line 104: Line 105:
</pre>
</pre>


== .gitignore ==
= Forks =
<pre>
 
*.log
= Configuration =
filename.txt
subfolder/*
</pre>


== hard reset to specific branch ==
=== Store credentials ===
<pre>
<pre>git config credential.helper store</pre>
git fetch --all
git reset --hard origin/branchname
</pre>


== Linux: Store credentials ==
=== Set username and email ===
<pre>
<pre>
git config credential.helper store
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
</pre>
</pre>


== Windows ==
=== Windows - remove old credentials ===
=== Remove old credentials ===
<pre>rundll32.exe keymgr.dll,KRShowKeyMgr</pre>
<pre>rundll32.exe keymgr.dll,KRShowKeyMgr</pre>


== Configuration ==
=== post-receive hook ===
* post-receive hook (execute script on main git server, after receiving push): hooks/post-receive
* execute script on main git server, after receiving push: hooks/post-receive
 
<blockquote>
<blockquote>
<pre>
<pre>
Line 161: Line 155:




= 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 ==
== Special features ==
* auto-commit when file changes
* auto-commit when file changes
Line 179: Line 181:
</blockquote>
</blockquote>
::or  
::or  
:* etckeeper
:* etckeeper or [https://github.com/gitwatch/gitwatch gitwatch]
::or
:* [https://github.com/gitwatch/gitwatch gitwatch]
 
== 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
 


[[Category:Linux/Services]]
[[Category:Linux/Services]]

Revision as of 17:25, 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


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