Git

Version

git --version

Configuration

Check

git config --list

Recommended

As your regular user...

git config --global user.name "FirstName LastName"

git config --global user.email "EmailAddress"

git config --global branch.autosetuprebase always

NOTE: Avoids merge commits for pulling

git config --global color.ui true

git config --global color.status auto

git config --global color.branch auto

git config --global core.editor vim

git config --global merge.tool vimdiff

Create new 'remote' repository

As 'root' on the server you want to be the remote git server...

For testing this can be a new user on your local machine but note that unless you have another user on another machine using this git server then you lose the key advantage of the distributed nature of git.

groupadd dev

useradd -G devs -d /home/gituser -m -s /bin/bash gituser

passwd gituser

As 'gituser' (in /home/gituser)...

mkdir project.git

cd project.git

git --bare init

As your regular user (assuming you do not already have a public key generated under your ~/.ssh directory...

ssh-keygen

ssh-copy-id -i ~/.ssh/id_rsa.pub gituser@gitserver

Create new 'local' repository

As your regular user (usually you would want to be doing this in your home directory)...

mkdir local_repo

cd local_repo

git init


Initialised empty Git repository in /home/user/local_repo/.git/

echo 'TODO: Add contents for README' > README

git status -s


?? README

git add .

git status -s


A  README

git commit -m 'Initial commit'


[master (root-commit) 5911c65] Initial commit 1 file changed, 1 insertion(+) create mode 100644 README

git log


commit 5911c65b6065f4004f6b102c299a119980f63421 (HEAD -> master)Author: FirstName LastName <email>Date:   Wed Mar 18 20:41:16 2020 +0000
    Initial commit

Configure 'remote' repository as 'remote'

As your regular user...

This needs to happen only once for each user who intends to use the 'remote' repository...

git remote add origin gituser@gitserver:project.git

git push origin master


Counting objects: 3, done.Writing objects: 100% (3/3), 244 bytes | 244.00 KiB/s, done.Total 3 (delta 0), reused 0 (delta 0)To gitserver:project.git * [new branch]      master -> master

Users & Groups

Usage

Workflow


https://www.tutorialspoint.com/git/git_life_cycle.htm

Clone

As your regular user...

mkdir local_repo

cd local_repo

git clone gituser@gitserver:project.git


gitserver:project.gitCloning into 'project'...remote: Counting objects: 3, done.remote: Total 3 (delta 0), reused 0 (delta 0)Receiving objects: 100% (3/3), done.

cd project

now, add or change files in this directory

Add to Staging Area

git status -s

git add file

Commit to Repository

git status -s

git commit –m "comment"

git log

Review

git log

git show commitid

make any changes

git diff

Git diff shows '+' sign before lines, which are newly added and '−' for deleted lines.

git status -s

git add file

git status -s

git commit --amend -m 'comment'

git log

git show commitid

git push origin master # Until this point, others cannot see the amendment

Two changes to same file

When two people make changes to the same file, the second user to try to push will face this situation...

git push origin master # will fail with an error

git pull # to refresh local repository

git push origin master

Stashing

To pause work and upload it to the repository without committing it...

git status -s

git stash        # Stash

git status -s

git stash list

git status -s

git stash pop    # Get it back to work on again (Pop)

Moving

To move a directory or file...

mkdir src

git mv file.c src/

git status -s

git commit -m "Modified directory structure"

git push origin master

Other users will see the change after the next git pull

Renaming

git commit -a -m 'Renamed file.c to string.c'

Note use of -a flag

git push origin master

Deleting

git rm filename

git commit -a -m "Removed filename"

git push origin master

Revert Uncommitted Changes

git checkout filename

Remove Changes from Staging Area

git checkout HEAD -- filename

Abandoning Changes

https://www.tutorialspoint.com/git/git_fix_mistakes.htm

Hosted Git repositories

GitLab

BitBucket