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 pullinggit 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
Usage
Workflow
Clone the Git repository as a working copy.
Modify the working copy by adding/editing files.
If necessary, you also update the working copy by taking other developer's changes.
Review the changes before commit.
Commit changes. If everything is fine, then you push the changes to the repository.
After committing, if you realize something is wrong, then you correct the last commit and push the changes to the repository.
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
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 flaggit 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.htmHosted Git repositories
Bibliography
https://git-scm.com/downloads https://www.tutorialspoint.com/git/git_basic_concepts.htmhttps://robdoylecreative.com/wp-content/uploads/2018/12/GIT-Cheatsheet-PDF.pdf https://michaeljswart.com/2018/07/the-bare-minimum-you-need-to-know-to-work-with-git/https://learngitbranching.js.org/
Fiddlershttps://git-school.github.io/visualizing-git/
GitLabhttps://about.gitlab.com/
BitBuckethttps://bitbucket.org/product/
Windowshttps://gitforwindows.org/