[GIT HELP] - Zephni ________________________________________________________________________________________________________________________________ + Basic commands ([var] is a variable, [ovar] os an optional variable) Change global config (Common settings) git config --global user.name "Your Name" git config --global user.email "your@email.com" git config --global color.ui auto Initate a repo, for starting a new one or initialising an existing one git init - or to make a bare repository git init --bare Clone a repo to local directory ([ovar] is a custom directory name for the cloned repo) git clone ssh://user@domain.com/git-directory-location [ovar] - or git clone http://domain.com/git-directory-location [ovar] Pull repo from a location ([var1] is the location, [var2] is the branch which is master by default) git pull [var1] [var2] - eg: git pull origin master Push repo to a location ([var1] is the location, [var2] is the branch which is master by default) git push [var1] [var2] - eg: git push origin master Add file or all files to current repo ([var] is filename) git add [var] - or to add all git add . Remove file from current repo ([var] is filename) git rm [var] Commit files to current repo, first with message ([var]), second with custom readme git commit -m "[var]" - or git commit View remote locations git remote -v Remove remote location ([var] is the alias for the location) git remote rm [var] Add remote location ([var1] is the alias for the location, [var2] is the public location such as SSH:// or HTTPS://) git remote add [var1] [var2] View branches (An asterix * will display before the branch you are currently in) git branch -a Create new branch and moving branches ([var] is branch name) git branch [var] - switch to branch git checkout [var] - create and switch to branch git checkout -b [vat] Merging branches, remember to move to the branch you want to merge to first as shown below ([var] is branch name) git checkout master git merge [var] Remove an old branch ([var] is branch name) git branch -d [var] Revert to an old commit ([var] is SHA1 hash) warning this will remove all commits to this point and revert to given commit git reset --hard [var] Checkout old commit and commit new ([var] is either SHA1 hash or HEAD~# where # is number of commits to go back). Note that the '.' dots are important. git rm -r . git checkout [var] . git commit -m "Reverted back to [var]" Count number of lines in each file in repository git ls-files | xargs wc -l ________________________________________________________________________________________________________________________________ + How To Set Up Automatic Deployment with Git on a public server 1. (On public server) - code below where unixuser is the directory that contains your public_html directory - We 'su unixuser' so that the correct user and permissions are applied to the live files Code: su unixuser cd /home/unixuser mkdir git && cd git git init --bare Info: This will set up a empty git repo that will recieve commits, we will now set up this repo to transfer its commited files to '/home/unixuser/public_html' live area when it recieves them. 2. (On public server) - Go into hooks and create a file named post-receive (cd hooks && nano post-recieve) and add the following code Code: #!/bin/sh git --work-tree=/home/unixuser/public_html --git-dir=/home/unixuser/git checkout -f Info: Obviously these directories will probably be different, the one on the left is the live area, and the one on the right is the now --bare repository 3. (On public server) - Make "post-recieve" executable with (chmod +x post-receive) 4. (One local server) - Now we need to add our to our local server, initiate git and set a 'live' remote location with SSH - Remember that this SSH user should have access to both the locations we created in the first step Code: cd /home/workspace mkdir mysite && cd mysite git init git remote add live ssh://unixuser@mydomain.com/~/git 5. (On local server) - Run the following commands to create a dummy file, add it to the local repo, commit the added objects and then push live Code: echo "I'm live!" > index.html git add . git commit -m "First commit" git push live master ________________________________________________________________________________________________________________________________ + How to ignore git files 1. Create / Edit .gitignore in root directory 2. Add line by line files/directories you wish to ignore 3. Use "git rm --cached filename" to all the files/directories we listed above 4. Use "git add ." and then "git status" to check it has deleted the unwanted files and added .gitignore 5. Commit changes ________________________________________________________________________________________________________________________________ SSH WITHOUT AUTHENTICATION Firstly run this command on the “local” machine ssh-keygen -t rsa Press enter to all of the questions to keep default settings (Files are stored in ~/.ssh/) Now use this command to put your public key in the authorized key file on the “remote” machine ssh-copy-id “user@remote.host -p 22”