Git basics (a few notes)

March 25, 2017 Douwe Jan Reitsma

********************************************************************************
Git basics
********************************************************************************

********************************************************************************
Versions may differ! in examples
********************************************************************************

 

www.digitalocean.com/community/tutorials/how-to-install-git-on-centos-7

********************************************************************************
Option One — Install Git with Yum
********************************************************************************

sudo yum install git
git –version

********************************************************************************
Option Two — Install Git from Source
********************************************************************************

sudo yum groupinstall “Development Tools”
sudo yum install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel
wget https://github.com/git/git/archive/v2.1.2.tar.gz -O git.tar.gz
tar -zxf git.tar.gz
cd git-*
make configure
./configure –prefix=/usr/local
sudo make install
git –version

********************************************************************************
Set Up Git
********************************************************************************

git config –global user.name “Your Name”
git config –global user.email “you@example.com”
git config –list
— user.name=Your Name
— user.email=you@example.com

 

www.digitalocean.com/community/tutorials/how-to-set-up-a-private-git-server-on-a-vps

********************************************************************************
Create the SSH Key Pair
********************************************************************************

ssh-keygen -C “youremail@mailprovider.com”
— Generating public/private rsa key pair.
— Enter file in which to save the key (/home/flynn/.ssh/id_rsa):
— Enter passphrase (empty for no passphrase):
— Enter same passphrase again:
— Your identification has been saved in foo_rsa.
— Your public key has been saved in foo_rsa.pub.
— The key fingerprint is:
— ab:cd:ef:01:23:45:67:89:0a:bc:de:f0:12:34:56:78 flynn@en.com

********************************************************************************
Setup a Git User and Install Git on your VPS
********************************************************************************

su –
useradd git
passwd git

********************************************************************************
Add your SSH Key to the Access List
********************************************************************************

su git
mkdir ~/.ssh && touch ~/.ssh/authorized_keys
cat .ssh/id_rsa.pub | ssh user@123.45.56.78 “cat >> ~/.ssh/authorized_keys”
cat ~/.ssh/authorized_keys

********************************************************************************
Setup a Local Repository (Server)
********************************************************************************

git init –bare my-project.git
— and
git remote set-url origin git@git.droplet.com:my-project.git
— or
git init && git remote add origin git@git.droplet.com:my-project.git
— or create a folder named after each person using Git
mkdir user1 user2
git remote add origin git@git.droplet.com:user1/user-project.git

 

www.digitalocean.com/community/tutorials/how-to-use-git-effectively

********************************************************************************
Creating your workspace
********************************************************************************

user@host ~ $ mkdir -p ~/git/testing ; cd ~/git/testing
user@host ~/git/testing $ touch file

********************************************************************************
Converting an existing project into a workspace environment
********************************************************************************

user@host ~/git/testing $ git init
— Initialized empty Git repository in /home/user/git/testing/.git/
user@host ~/git/testing $ git add .

********************************************************************************
Converting an existing project into a workspace environmentCreating a commit message
********************************************************************************

user@host ~/git/testing $ git commit -m “Initial Commit” -a
— [master (root-commit) 1b830f8] initial commit
— 0 files changed
— create mode 100644 file
user@host ~/git/testing $ git commit -m “Initial Commit” file

********************************************************************************
Pushing changes to a remote server
********************************************************************************

user@host ~/git/testing $ git remote add origin ssh://git@git.domain.tld/repository.git
user@host ~/git/testing $ git remote -v
— origin ssh://git@git.domain.tld/repository.git (fetch)
— origin ssh://git@git.domain.tld/repository.git (push)
user@host ~/git/testing $ git push origin master
— Counting objects: 4, done.
— Delta compression using up to 2 threads.
— Compressing objects: 100% (2/2), done.
— Writing objects: 100% (3/3), 266 bytes, done.
— Total 3 (delta 1), reused 1 (delta 0)
— To ssh://git@git.domain.tld/repository.git
— 0e78fdf..e6a8ddc master -> master

 

www.digitalocean.com/community/tutorials/how-to-use-git-branches

********************************************************************************
Viewing branches
********************************************************************************

git branch -a
— * master
— remotes/origin/master

********************************************************************************
Creating branches
********************************************************************************

git checkout -b develop
— Switched to a new branch ‘develop’
— fatal: A branch named ‘develop’ already exists.
git checkout master
git checkout develop
— Switched to branch ‘master’
git checkout nosuchbranch
error: pathspec ‘nosuchbranch’ did not match any file(s) known to git.

********************************************************************************
Making changes to our develop branch
********************************************************************************

git checkout develop
touch develop
git add develop
git commit -m “develop file” develop
git branch
— * develop
— master
ls
— develop file

********************************************************************************
Merging code between branches
********************************************************************************

git checkout master
git branch
— develop
— * master
ls
— new file is missing
git merge develop –no-ff
— Merge made by the ‘recursive’ strategy.
— 0 files changed
— create mode 100644 develop
ls
— develop file
git push
— Counting objects: 4, done.
— Delta compression using up to 2 threads.
— Compressing objects: 100% (3/3), done.
— Writing objects: 100% (3/3), 332 bytes, done.
— Total 3 (delta 1), reused 0 (delta 0)
— To ssh://git@git.domain.tld/repository
— 9af2dcb..53649cf master -> master

********************************************************************************
End Git basics
********************************************************************************

 

********************************************************************************
Git configuration Douwe
********************************************************************************

********************************************************************************
Working directories on server
********************************************************************************

/home/net/ict-infra.nl/ansible
/home/net/ict-infra.nl/git

drwxrwx—. 2 root psacln 6 Dec 10 21:23 ansible
drwxrwx—. 3 root psacln 27 Dec 11 10:48 git

********************************************************************************
Setting up a Local Repository (Server)
********************************************************************************

$ cd /home/net/ict-infra.nl/git

$ sudo yum install git

$ git –version

git version 1.8.3.1

$ ls -latr


drwxrwx—. 4 root psacln 30 Dec 10 21:14 ..
drwxrwx—. 2 root psacln 6 Dec 12 13:48 .

$ git init –bare ictinfra-project.git

Initialized empty Git repository in /home/net/ict-infra.nl/git/ictinfra-project.git/

$ ls -latr


drwxrwx—. 4 root psacln 30 Dec 10 21:14 ..
drwxrwx—. 3 root psacln 33 Dec 12 14:32 .
drwxr-xr-x. 7 nldourei1 psacln 4096 Dec 12 14:32 ictinfra-project.git

As user root …

# chown -R nldourei1:psacln ictinfra-project.git)

As default user …

$ git config –global user.name “Douwe Jan Reitsma”
$ git config –global user.email “douwejanreitsma@gmail.com”

from /home/net/ict-infra.nl/git

$ git config –list

user.name=Douwe Jan Reitsma
user.email=douwejanreitsma@gmail.com

from /home/net/ict-infra.nl/git/ictinfra-project.git

$ git config –list

user.name=Douwe Jan Reitsma
user.email=douwejanreitsma@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=true

********************************************************************************
Connecting the Local Repository (Server) from a Local Computer
(assuming SSH is configured and Git installed on the local computer).
********************************************************************************

$ mkdir -p /Users/douwejanreitsma/Projects/ictinfra-project

$ pwd

/Users/douwejanreitsma/Projects/ictinfra-project

$ cd /Users/douwejanreitsma/

$ chmod -R 770 Projects/

$ cd Projects/ictinfra-project/

$ git init && git remote add origin nldourei1@<ip-address provider>:/home/net/ict-infra.nl/git/ictinfra-project.git

Initialized empty Git repository in /Users/douwejanreitsma/Projects/ictinfra-project/.git/

$ git config –global user.name “Douwe Jan Reitsma”

$ git config –global user.email “douwejanreitsma@gmail.com”

$ git config –list

user.name=Douwe Jan Reitsma
user.email=douwejanreitsma@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=nldourei1@<ip-address provider>:/home/net/ict-infra.nl/git/ictinfra-project.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

********************************************************************************
Sample: Testing Git.
Connecting the Local Repository (Server) from a Local Computer.
********************************************************************************

$ git checkout -b origin

Switched to a new branch ‘origin’

$ vi test.txt

$ git add test.txt

$ git status

On branch origin
Initial commit
Changes to be committed:
(use “git rm –cached …” to unstage)
new file: test.txt

$ git commit -m “test.txt” test.txt

[origin (root-commit) ac214bc] test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt

$ git status

On branch origin
Your branch is up-to-date with ‘origin/origin’.
nothing to commit, working directory clean

$ git push

— Some warnings appeared about usage:
— git config –global push.default simple
— git push –set-upstream origin origin

$ git –version

git version 2.5.4 (Apple Git-61)

$ git config –global push.default simple

$ git push –set-upstream origin origin

Counting objects: 3, done.
Writing objects: 100% (3/3), 228 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To nldourei1@<ip-address provider>:/home/net/ict-infra.nl/git/ictinfra-project.git
* [new branch] origin -> origin
Branch origin set up to track remote branch origin from origin.

$ git branch -a

* origin
remotes/origin/origin

********************************************************************************
Some History. There is much more … think about merging
branches (develop with master) and others …
In the sites, given above, you can find a lot of info.
There are also some training sites
********************************************************************************

git checkout -b origin
git add Documents/
git status
git commit -m “Documents” Documents
git status
git push –set-upstream origin origin
git push
git branch -a
***
git checkout -b develop
git push –set-upstream origin develop
git branch -a
***
git checkout -b master
git push –set-upstream origin master
git branch -a
ls -latr
***
git checkout develop
ls -latr
***
git checkout master
ls -latr
***
git checkout origin
ls -latr
***
git checkout develop
git rm -rf Documents
git commit -m “Remove Documents from develop” Documents
git push
ls -latr
***
git checkout master
git rm -rf Documents
git commit -m “Remove Documents from master” Documents
git push
ls -latr
***
git checkout origin
ls -latr
ll Documents/Books & Notes/

********************************************************************************
End Git configuration Douwe
********************************************************************************

Related Entries