0% found this document useful (0 votes)
29 views34 pages

Git

The document is a comprehensive guide to Git and GitLab, covering installation, setup, repository creation, and various Git operations such as committing, branching, merging, and handling conflicts. It also explains how to connect to remote repositories using SSH and outlines the basics of GitLab CI for continuous integration. The document serves as a starter kit for users looking to understand and utilize Git and GitLab effectively.

Uploaded by

ragulm0122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views34 pages

Git

The document is a comprehensive guide to Git and GitLab, covering installation, setup, repository creation, and various Git operations such as committing, branching, merging, and handling conflicts. It also explains how to connect to remote repositories using SSH and outlines the basics of GitLab CI for continuous integration. The document serves as a starter kit for users looking to understand and utilize Git and GitLab effectively.

Uploaded by

ragulm0122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

GIT & GITLAB - STARTERKIT-2022

28th Nov 2022


Shrija Sheth

03/25/2025 2
• What is Git and Gitlab?
Outline • How to download Git?
• Setting up Git
• Creating a Repo
• How does Git work?
• Git Unstage files and Tracking
Changes
• Committing a folder, Deleting, Ignore
• Git Branch, Merges & Conflicts
• Remotes in CERN Gitlab
• Gitlab CI

03/25/2025 3
What is Git/GitLab/GitHub?
• Git: Version Control System.
• GitLab/GitHub: Source Code
management
System/platform used to
host git repositories and to
share it with others enabling
collaboration.
• GitLab has a built-in CI/CD
whereas GitHub doesn’t.

03/25/2025 4
Download Git

📚 - Resources
•Git installation instructions
•Setup Git for Windows 10
•Setup Git for macOS
•Setup Git for Linux

03/25/2025 5
Setting up Git
• $ git --version
• $ git config --global user.name “Sherlock Holmes”
• $ git config --global user.email “[email protected]
• $ git config -h (list of commands)
• $ git config --help (git manual)

03/25/2025 6
Creating a Repository
• $ mkdir gitlab (make a directory - repository)
• $ cd gitlab
• $ git init (to initialise git)
• $ vim readme.md (Press ‘i’ to go to insert mode, press ’escape’ to
come out of insert mode. Press ‘:x’ to save and exit)
• $ ls (to list items)
• $ ls -a (to show hidden directory)
• $ git status (to show on which branch are you)
• $ git add . (sends file to staging area)
• $ git commit -m “my initial commit” (sends file to local repo)

03/25/2025 7
How does Git work?

03/25/2025 8
How does Git work?

03/25/2025 9
Git Unstage files
• vim index.html
• vim readme.md (”Hello! I am learning git”)
• git status
• git add .
• git commit -m “Added index.html and readme”
• git status
• git reset HEAD readme.md (Note: Removes from the staging area and it doesn’t
mean that the changes have gone away. Need to commit)
• git status
• git commit -m “added first page” (Commits only index.html)
• git status
• git add .
• git commit -m “added readme”
• git status

03/25/2025 10
Track Changes
• $ git status
• $ git log (shows the time and date of each
commit)
• $ git log --patch (shows the details of the
file)
• $ git diff (helps to review changes)
• $ git diff --staged (Press q to quit)

03/25/2025 11
Committing a folder
• mkdir temp
• ls -a
• git status
• touch temp/.gitkeep (Create an empty file inside an empty folder)
• git status
• git add .
• git commit -m “Added a temp folder”
• git status

03/25/2025 12
Delete Files
• touch newfile.txt
• ls -a
• git status
• rm newfile.txt
• git status
• Question: Will it be removed?

03/25/2025 13
Delete Folder
• rm -rf -- temp
• ls -a
• git status
• git add .
• git commit -m “removed file and folder”

03/25/2025 14
Ignore files (For private files not in repo)
• mkdir config
• touch config/private.txt
• git status
• vim .gitignore (config/ - specify files and folders you want to ignore)
• git status
• ls -a
• git add .
• git commit -m “Added gitignore config”
• git status

03/25/2025 15
Git Branch
• git checkout -b feature/new-table
• git status
• vim index.html (Make some changes in the branch feature/new-table)
• git add .
• git commit -m “Added a table”
• cat index.html
• git checkout master
• cat index.html
• git checkout feature/new-table
• git branch -d feature/new-table

03/25/2025 16
Git Merge
1. Fast-forward merge
• git checkout master
• git merge feature/new-table
• git log
• git branch
• git branch -d feature/new-table

03/25/2025 17
Fast-forward Merge

03/25/2025 18
Git Merge
2. Advance merge
• git checkout -b bugfix/table
• vim index.html
• git add .
• git status
• git commit -m “Added table cell”
• git checkout master
• vim readme.md (I am learning and merging)
• git add .
• git commit -m “Learning branching”

03/25/2025 19
Git Merge
2. Advance merge - continue… (Rebase Commits)
• git checkout -b bugfix/table-2
• vim index.html
• git add .
• git commit -m “Added 2nd cell”
• git checkout master
• vim readme.md (“I am learning, merging and rebasing”)
• git add .
• git commit -m “Learning rebasing”
• git checkout bugfix/table-2
• git log
• git rebase master
• git log
• git checkout master
• git merge bugfix/table-2

03/25/2025 20
Advanced Merge

03/25/2025 21
Merge Conflicts
• git merge --abort
• git rebase --abort
• Go to the IDE (code .) and accept the
changes
• Then merge
• git checkout master
• git merge branch2

03/25/2025 22
Remotes in CERN Gitlab- SSH
• Generate an SSH key pair:
• For example, for ED25519:
1. ssh-keygen -t ed25519 -C "<comment>"
• For 2048-bit RSA:
2. ssh-keygen -t rsa -b 2048 -C "<comment>"
• Generating public/private ed25519 key pair.
• Enter file in which to save the key (/home/user/.ssh/id_ed25519):

03/25/2025 23
Remotes in CERN Gitlab- SSH
Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:
• macOS
tr -d '\n' < ~/.ssh/id_ed25519.pub | pbcopy
• Linux (requires the xclip package)
xclip -sel clip < ~/.ssh/id_ed25519.pub
• Git Bash on Windows
cat ~/.ssh/id_ed25519.pub | clip
Replace id_ed25519.pub with your filename. For example, use id_rsa.pub for RSA.

1. Sign in to GitLab.
2. On the top bar, in the top right corner, select your avatar.
3. Select Preferences.
4. On the left sidebar, select SSH Keys.
5. In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with ssh-rsa, ssh-dss, ecdsa-sha2-
nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521, ssh-ed25519, [email protected], or [email protected], and may end with a comment.
6. In the Title box, type a description, like Work Laptop or Home Workstation.
7. Optional. Update Expiration date to modify the default expiration date. In:
1. GitLab 13.12 and earlier, the expiration date is informational only. It doesn’t prevent you from using the key. Administrators can view expiration dates and use them for guidance
when deleting keys.
2. GitLab checks all SSH keys at 02:00 AM UTC every day. It emails an expiration notice for all SSH keys that expire on the current date. ( Introduced in GitLab 13.11.)
3. GitLab checks all SSH keys at 01:00 AM UTC every day. It emails an expiration notice for all SSH keys that are scheduled to expire seven days from now. ( Introduced in GitLab
13.11.)
8. Select Add key.

03/25/2025 24
Remotes in CERN Gitlab- SSH
• If this is the first time you connect, you should verify the authenticity of the GitLab host. If you
see a message like:

The authenticity of host 'gitlab.example.com (35.231.145.151)' can't be established.


ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.example.com' (ECDSA) to the list of known hosts.

• Type yes and press Enter.

• Run the ssh -T [email protected] command again. You should receive a


• Welcome to GitLab, @username! message. Everything is working correctly if the output of the
command is:
• Welcome to GitLab, @sherlock!

03/25/2025 25
Remotes in CERN Gitlab- SSH
• If the welcome message doesn’t appear, you
can troubleshoot by running ssh in verbose
mode:
• ssh -Tvvv [email protected]

03/25/2025 26
Remotes in CERN Gitlab- Git Push
• git remote add origin
ssh://[email protected]:7999/ssheth/my-cool-
web.git
• git add .
• git commit -m ”Final commit"
• git push -u origin master

03/25/2025 27
Remotes in CERN Gitlab- Git Pull
• git clone ssh://[email protected]:7999/ssheth/my-cool-
web.git
• git pull origin master
• vim pluto.txt
• cat pluto.txt
• git add .
• git commit -m ”commit-2"
• git push -u origin master
03/25/2025 28
Remotes in CERN Gitlab- Git Clone
• mkdir test_merge_requests/
• cd test_merge_requests/
• git clone ssh://[email protected]:7999/starter-kit-scratch/starter-
kit-example.git
• cd starter-kit-example/
• git remote add YOUR_CERN_USERNAME
ssh://[email protected]:7999/YOUR_CERN_USERNAME/starter-kit-
example.git
• git pull / git pull origin

03/25/2025 29
Remotes in CERN Gitlab- Git Clone
• In some cases you might end up with your working directory (and/or
branch) “messed up” and the git pull command will not work seamlessly. If
you are sure you are not going to lose anything important, you can simply
reset your current working directory by ignoring any local modification
and destroying it:
• git fetch --all # make local git aware of what changed remotely
• git reset --hard origin/master # lose any local modification
• git clean -fxd # get rid of any extra files not under version control
• You should be very careful about using
the git reset and git clean commands, but they are very useful when your
working area got messed up somehow.
• echo "I hereby certify that this information is true." >> YOUR_CERN_USERNAME.txt
• git commit -a -m 'Add information certification’
• git push YOUR_CERN_USERNAME
03/25/2025 30
Summary
• A local Git repository can be connected to one or more remote
repositories.
• Use the SSH protocol to connect to remote repositories.
• git push copies changes from a local repository to a remote
repository.
• git pull copies changes from a remote repository to a local
repository. In practice, it is good to be sure that you have an
updated version of the repository you are collaborating on, so you
should git pull before making our changes. The basic collaborative
workflow would be:
• update your local repo with git pull origin master,
• make your changes and stage them with git add,
• commit your changes with git commit -m, and
• upload the changes to GitLab with git push origin master
03/25/2025 31
GitLab CI
• Continuous integration (or CI) is a popular development
practice where one or more tasks are ran automatically,
often each time a commit is pushed to a remote
repository.
• Used for Running tests for software packages or deploying
changes.
• The .gitlab-ci.yml file is used to configure CI.
• “Pipeline failed" means you can not merge because GitLab
CI test has failed
(https://gitlab.cern.ch/lhcb/Alignment/-/pipelines).
• Practice:
https://hsf-training.github.io/analysis-essentials/git/11-
ci.html
03/25/2025 32
Thanks for your attention

03/25/2025 33

You might also like