GIT is a distributed version control system that was created by Linus Torvalds, the mastermind of Linux itself. It was designed to be a superior version control system to those that were readily available, the two most common of these being CVS and Subversion (SVN). Whereas CVS and SVN use the Client/Server model for their systems, GIT operates a little differently. Instead of downloading a project, making changes, and uploading it back to the server, GIT makes the local machine act as a server. Tecmint

The basics

 1# Clone an existing Projet: 
 2git clone https://github.com/MozeBaltyk/bac-a-sable
 3
 4# Save your changes:
 5cd bac-a-sable/
 6touch file.txt
 7git add bac-a-sable/
 8git commit -m "start with a verb then on something"
 9git push origin master
10
11# Not recommand but take everything
12git add -A

Branches

 1# List local branch
 2git branch
 3master
 4* new_branch
 5
 6# List remote branch
 7git branch -r
 8master
 9* new_branch
10
11# Switch
12git checkout new_branch
13Switched to branch 'new_branch'
14
15#Create 
16git checkout -b rookSetup
17git push --set-upstream origin rookSetup
18
19# Delete 
20git branch -D test_branch
21Deleted branch test_branch (was 5776472).
22git push origin :test_branch
23
24#Merge Branches dev with master
25git checkout master 
26git merge dev 

Rollback

1#Quit a commit
2git rebase --skip
3git rebase origin/master
4
5# Rollback a file
6git checkout -- filename.yml

Investigate

1git log
2git log -p <commit_nbr>
3git log --oneline --decorate --graph --all   : Voir en graphes tous les commits 

Resolve conflict

During a push, you realise that someone already pushed, so git ask you to pull first

1AnsiColt git:main โฏ git push
2To ssh://github.com/MozeBaltyk/AnsiColt.git
3 ! [rejected]        main -> main (fetch first)
4error: failed to push some refs to 'ssh://github.com/MozeBaltyk/AnsiColt.git'
5hint: Updates were rejected because the remote contains work that you do
6hint: not have locally. This is usually caused by another repository pushing
7hint: to the same ref. You may want to first integrate the remote changes
8hint: (e.g., 'git pull ...') before pushing again.
9hint: See the 'Note about fast-forwards' in 'git push --help' for details.

During pull, it possible you did not set the rebase

 1AnsiColt git:main โฏ git pull 
 2remote: Enumerating objects: 5, done.
 3remote: Counting objects: 100% (3/3), done.
 4remote: Total 5 (delta 3), reused 3 (delta 3), pack-reused 2
 5Unpacking objects: 100% (5/5), 839 bytes | 279.00 KiB/s, done.
 6From ssh://github.com/MozeBaltyk/AnsiColt
 7   52a5d9a..38bea24  main       -> origin/main
 8hint: You have divergent branches and need to specify how to reconcile them.
 9hint: You can do so by running one of the following commands sometime before
10hint: your next pull:
11hint:
12hint:   git config pull.rebase false  # merge (the default strategy)
13hint:   git config pull.rebase true   # rebase
14hint:   git config pull.ff only       # fast-forward only
15hint:
16hint: You can replace "git config" with "git config --global" to set a default
17hint: preference for all repositories. You can also pass --rebase, --no-rebase,
18hint: or --ff-only on the command line to override the configured default per
19hint: invocation.
20fatal: Need to specify how to reconcile divergent branches.

For me the best is to set it to false, to manually resolved conflit. That’s a carefull approach.

1AnsiColt git:main โฏ git config pull.rebase false

The pull will then indicate which files have conflits, so you can take a look and solve those conflits

 1AnsiColt git:main โฏ git pull
 2Auto-merging .github/workflows/ci.yml
 3CONFLICT (content): Merge conflict in .github/workflows/ci.yml
 4Automatic merge failed; fix conflicts and then commit the result.
 5
 6AnsiColt git:main โฏ vi .github/workflows/ci.yml 
 7
 8AnsiColt git:main โฏ git add -A
 9AnsiColt git:main โฏ git commit -m "desactivate workflow"
10[main 75b47c2] desactivate workflow
11AnsiColt git:main โฏ git push

My .gitconfig

Set some usefull aliases inside your ~/.gitconfig:

 1[alias]
 2    br = branch
 3    co = checkout
 4    ci = commit
 5    st = status
 6
 7    # pretty and compact log with colours:
 8    lg = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)'
 9
10[color]
11    # enable colours for diff, log, etc.
12    ui = true

Git submodules

 1# Initialisation a new project (here an ansible collection)
 2git clone https://<username>:<token>@gitlab.example.com/group/namespace.general.git
 3ansible-galaxy collection init namespace.general
 4git add -A &&  git commit -m "Initialisation" && git push 
 5
 6# Add submodules 
 7git submodule add https://<username>:<token>@gitlab.example.com/group/namespace.another.git
 8git submodule add https://<username>:<token>@gitlab.example.com/group/namespace.second.git
 9git add -A &&  git commit -m "Initialisation" && git push 
10
11# Update submodules
12cd namespace/general
13git pull --recurse-submodules     #Fetch and show if there were changes from submodule
14git submodule update --remote --merge
15git add -A
16git commit -am "message"
17git push
18
19# Update to last tag
20cd namespace/general/my/submodule
21git fetch && git tag | tail -1
22git checkout $(git tag | tail -1) 
23cd ../..
24git add my/submodule
25git commit -m "update submodules"
26git push
27
28# Remove a submodule
29git submodule deinit [submodule-path]
30git rm --cached [submodule-path]
31rm -rf .git/modules/test
32git config --remove-section submodule.test