Fundamentals
1. Initialize a new repository & create a new Github
account
Command: git init
Explanation: This command sets up a new Git repository in your current
directory. It creates a hidden .git directory that tracks all changes.
Example:
cd omics
git init
This command initialises a new Git repository in the omics directory.
2
Access GitHub account
Generate and Add an SSH Key
1. Open Terminal: Open the terminal on your computer.
2. Generate SSH Key:
● Run the following command, replacing your email address with the email
you used for GitHub:
● Press Enter to accept the default file location.
● Enter a secure passphrase when prompted.
3. Add SSH Key to ssh-agent:
● Start the ssh-agent in the background:
eval "$(ssh-agent -s)"
● Add your SSH private key to the ssh-agent:
ssh-add ~/.ssh/id_ed25519
4. Add SSH Key to GitHub:
● Copy the SSH key to your clipboard:
cat ~/.ssh/id_ed25519.pub
● Go to GitHub and navigate to Settings > SSH and GPG keys.
● Click the New SSH key, give it a title, and paste your SSH key into the “Key”
field. Click Add SSH key.
Create a Personal Developer Token
1. Navigate to Settings:
3
● Go to your GitLab account settings by clicking on your profile picture at the
top right and selecting Profile Settings.
2. Access Token:
● On the right sidebar, click on Access token.
3. Personal Access Tokens:
● Click on Personal access tokens and then Add new token.
4. Generate New Token:
● Click on Add new token.
● Give your token a descriptive name.
● Select the scopes or permissions you need. For example, to use the
token for repository access, check the box for repo.
● Click Create personal access token.
5. Save Your Token:
● Copy the token and save it somewhere secure. This token will only be
shown once.
2. Clone an Existing Repository
Command: git clone [repository URL]
Explanation: This command copies an existing repository from a remote server
(like GitHub) to your local machine.
4
Example:
git clone http://git.merai.app:8929/merai-apps/omics.git
This command clones the repository at https://git.merai.app/user/repo.git into a
new directory named repo.
3. Check the Status of Your Repository
Command: git status
Explanation: This command shows you the current state of your repository,
including any changes that have been staged, changes that haven’t been staged,
and files that Git isn’t tracking.
Example:
git status
This will output the status of your files, letting you know what has changed since
the last commit.
5
4. Add Files to the Staging Area
Command: git add [file]
Explanation: This command stages files to be included in the next commit. You
can stage individual files or all changes at once.
Example:
git add index.js
This command stages index.js for the next commit.
To stage all changes:
git add .
6
5. Commit Changes
Command: git commit -m "[message]"
Explanation: This command takes a snapshot of the changes in the staging area
and records it in the repository with a message describing what was changed.
Example:
git commit -m "Initial commit"
This command creates a commit with the message “Initial commit”.
6. Push Changes to a Remote Repository
Command: git push [remote] [branch]
Explanation: This command uploads your local commits to a remote repository.
Example:
git push origin main
This command pushes your changes to the main branch of the remote repository
named origin.
7
7. Pull Changes from a Remote Repository
Command: git pull [remote] [branch]
Explanation: This command fetches changes from a remote repository and
merges them into your current branch.
Example:
git pull origin main
This command fetches and merges changes from the main branch of the remote
repository named origin.
Branching and Merging
8. List All Local Branches
Command: git branch
8
Explanation: This command lists all branches in your repository.
Example:
git branch
This command shows a list of all branches, with the current branch highlighted.
9. Create a New Branch
Command: git branch [branch-name]
Explanation: This command creates a new branch in your repository.
Example:
git branch new-branch
This command creates a new branch named new-branch.
9
10. Switch to a Different Branch
Command: git checkout [branch]
Explanation: This command switches to the specified branch.
Example:
git checkout new-branch
This command switches to the new-branch.
To create and switch to a new branch in one step:
git checkout -b new-branch
11. Merge Branches
Command: git merge [branch]
Explanation: This command merges the specified branch into the current branch.
Example:
git checkout main
git merge new-branch
This command merges the new-branch into the main branch.
10
Remote Repositories
12. Add a Remote Repository
Command: git remote add [name] [URL]
Explanation: This command adds a new remote repository to your configuration.
Example:
git remote add origin https://github.com/user/repo.git
This command adds a remote repository named origin with the URL
https://github.com/user/repo.git.
13. View Remote Repositories
Command: git remote -v
11
Explanation: This command displays the URLs of all remote repositories.
Example:
git remote -v
This command shows the URLs for all configured remote repositories.
Undoing Changes
14. Unstage a File
Command: git reset [file]
Explanation: This command removes a file from the staging area but keeps its
changes in the working directory.
Example:
git reset test.js
This command unstages test.js.
12
15. Discard Changes in a File
Command: git checkout -- [file]
Explanation: This command discards changes in the working directory, reverting
the file to the last committed state.
Example:
git checkout -- test.js
This command reverts test.js to its state at the last commit.
16. Revert a Commit
First, find the commit hash you want to revert:
Command: git log
Explanation: This command will show you the commit history. Look for the
commit hash (a long string of characters) of the commit you want to revert.
Command: git revert [commit]
Explanation: This command creates a new commit that undoes the changes from
a specified commit.
Example:
git revert abc123
This command reverts the commit with the ID abc123.
13
Edit commit message in Vim editor:
After running the git revert command, vim will open to allow you to edit the
commit message for the revert commit.
● Make any necessary changes to the commit message.
● Save and close the vim editor:
● Press i to enter insert mode and make your changes.
● Press Esc to exit insert mode.
● Type :wq and press Enter to save and quit Vim.
14
Viewing History
17. View Commit History
Command: git log
Explanation: This command shows the commit history for the current branch.
Example:
git log
This command displays a list of all commits.
To see a one-line summary of each commit:
git log --oneline
15
Stashing Changes
18. Stash Changes
Command: git stash
Explanation: This command temporarily saves changes that are not ready to be
committed.
Example:
git stash
This command stashes your uncommitted changes.
Command: git stash apply
Explanation: This command reapplies the stashed changes.
16
19. Apply Stashed Changes
Command: git stash pop
Explanation: This command applies the most recently stashed changes and removes
them from the stash list.
Example:
git stash pop
This command applies the stashed changes and removes them from the stash.
20. List Stashes
Command: git stash list
Explanation: This command lists all stashed changes.
Example:
git stash list
This command displays a list of all stashes.
Advanced Git
21. Rebase Branches
Command: git rebase [branch]
Explanation: Rebase allows you to move or combine a sequence of commits to a new
base commit. This is often used to keep a feature branch up to date with the latest
changes in the main branch.
Example:
git checkout feature-branch
git rebase main
17
This command rebases the feature-branch onto the latest commit in the main branch.
22. Interactive Rebase
Command: git rebase -i [commit]
Explanation: Interactive rebase lets you modify commits (e.g., squash, reword) in your
branch history.
Example:
git rebase -i HEAD~3
This command opens an interactive editor for the last three commits, allowing you to
squash, edit, or reorder them.
23. Cherry-Pick Commits
Command: git cherry-pick [commit]
Explanation: Cherry-pick applies the changes from a specific commit onto your current
branch.
Example:
git cherry-pick abc123
This command applies the changes from commit abc123 to the current branch.
24. Tagging Commits
Command: git tag [tag-name]
Explanation: Tags are used to mark specific points in your commit history, often used
for releases.
18
Example:
git tag v1.0.0
This command creates a tag named v1.0.0 at the current commit.
To push tags to a remote repository:
git push origin --tags
25. Fetching and Merging
Command: git fetch and git merge [branch]
Explanation: git fetch downloads objects and refs from another repository, while git
merge integrates changes into your current branch.
Example:
git fetch origin
git merge origin/main
This command fetches the latest changes from the main branch of the remote
repository and merges them into your current branch.
26. Handling Conflicts
Scenario: When merging or rebasing, you might encounter conflicts if changes overlap.
Steps to Resolve:
● Identify conflicted files using git status.
● Open the conflicted file and look for conflict markers (<<<<<<<, =======,
>>>>>>>).
● Edit the file to resolve conflicts.
● Stage the resolved files using git add.
19
● Continue the merge or rebase process using git commit or git rebase --continue.
Example:
git status
# Edit the conflicted files
git add resolved-file.txt
git commit -m "Resolved merge conflict"
27. Using Submodules
Command: git submodule
Explanation: Submodules allow you to include and manage repositories within other
repositories.
Adding a Submodule:
git submodule add https://github.com/user/repo.git path/to/submodule
This command adds a submodule from the specified repository to the given path.
Updating Submodules:
git submodule update --remote
This command updates the submodules to the latest commit.
28. Managing Large Files with Git LFS
Command: git lfs
Explanation: Git Large File Storage (LFS) manages large files such as datasets or AI
models in a Git repository.
Installation:
20
git lfs install
Tracking a File:
git lfs track "large-file.zip"
This command tells Git LFS to manage the specified large file.
Pushing Large Files:
git add large-file.zip
git commit -m "Add large file"
git push origin main
29. Automating Workflows with Git Hooks
Explanation: Git hooks are scripts that run automatically on specific git events (e.g.,
commit, push).
Example: Pre-Commit Hook:
● Navigate to the .git/hooks directory.
● Create a file named pre-commit.
● Add the script you want to run before each commit.
Sample Pre-Commit Hook:
#!/bin/sh
# Check for Python syntax errors
find . -name "*.py" | xargs pylint
This script runs pylint on all Python files before committing.
30. Continuous Integration (CI) with GitHub Actions
Explanation: GitHub Actions automates workflows, including testing and deployment,
directly from your GitHub repository.
21
Example: Setting Up a CI Pipeline:
● Create a directory named .github/workflows.
● Create a YAML file (e.g., ci.yml) in this directory.
Sample YAML for CI Pipeline:
This configuration runs a CI pipeline that checks out the code, sets up Python, installs
dependencies, and runs tests on every push and pull request to the main branch.
22
Integrating AI Models in Web Development
31. Versioning AI Models
Explanation: When integrating AI models in a web development project, it’s crucial to
version control the models for consistency and reproducibility.
Example:
● Train and save your model.
● Use Git LFS to track the model file.
Commands:
git lfs track "models/model-v1.pkl"
git add models/model-v1.pkl
git commit -m "Add trained model version 1"
git push origin main
32. Automating Model Deployment
Explanation: Automate the deployment of AI models using CI/CD tools.
Example with GitHub Actions:
● Extend your CI pipeline to include deployment steps.
● Use a platform like Heroku, AWS, or Azure for deployment.
Sample Deployment Step:
23
This step deploys your application to AWS after successful tests.
33. Collaborating with Teams
Explanation: Effective collaboration involves branching strategies, code reviews, and pull
requests.
Steps:
● Create a new branch for your feature.
● Commit and push changes.
● Open a pull request (PR) on GitHub for code review.
Commands:
git checkout -b feature-ai-integration
# Make changes, add, and commit
git push origin feature-ai-integration
24
34. Reviewing and Approving Pull Requests
Explanation: Use GitHub’s PR interface to review code, leave comments, and approve
changes.
Steps:
● Navigate to the PR in your repository.
● Review the changes.
● Leave comments or approve the PR.
Example:
● Reviewer leaves comments or requests changes.
● Once approved, the PR is merged into the main branch.
35. Setting Up Protected Branches
Explanation: Protect critical branches (e.g., main) to enforce policies like required
reviews and CI checks.
Steps:
● Go to the repository settings on GitHub.
● Navigate to Branches under “Code and automation”.
● Add branch protection rules.
Example:
● Require pull request reviews before merging.
● Require status checks to pass before merging.