Git & GitHub
Git & GitHub
Why use Git? Git is a distributed version control system that offers several
advantages:
• It allows for non-linear development (parallel branches)
• It provides strong support for collaborative development
• It's fast and efficient for both small and large projects
• It ensures data integrity and security
These steps are crucial for setting up Git on your local machine and preparing it
for use with version control and collaboration on projects.
git log: Displays the commit history, showing our initial commit.
These commands form the basic workflow for tracking changes in a Git
repository.
In this example:
We make changes, stage them with git add , and commit them.
Finally, we merge the 'feature' branch into 'master' using git merge .
This workflow allows for parallel development and safe experimentation without
affecting the main codebase until the changes are ready to be merged.
2. Creating a GitHub account: Visit github.com and sign up for a new account.
You'll need to provide a username, email address, and password.
3. Creating a new repository on GitHub: After logging in, click the '+' icon in
the top-right corner and select "New repository". Give your repository a name
(e.g., "my-first-repo"), choose whether it should be public or private, and click
"Create repository".
Once you've created your repository, GitHub will provide instructions on how to
push an existing repository from the command line:
This allows you to interact with both the original repository and your fork.
2. git push: Pushing changes to GitHub
This creates a new branch with your changes and pushes it to your fork on
GitHub.
3. git pull: Pulling changes from GitHub
This ensures your local main branch is up-to-date with the original project.
1. Forking a repository
Make changes to the code, commit them, and push to your fork:
Go to your fork on GitHub and click "Compare & pull request". Fill in the
description and create the pull request.
If changes are requested, make the necessary updates in your local branch,
commit, and push. The pull request will automatically update.
Once approved, the maintainer can merge your pull request, incorporating your
changes into the main project.
This example demonstrates how forking, creating pull requests, and the review
process work together to facilitate collaboration on GitHub.
Avoid Vague Messages: Specific details help others (and your future
self) understand the change.
Global .gitignore: Apply a global .gitignore for files that should never be
committed across any repository.
Placement: The .gitignore file should reside in the root directory of your
project to apply to the entire repository.
Push Regularly: Regular pushes prevent merge conflicts and keep your
work backed up and in sync with the team.
Review Before Committing: Ensure that only the intended changes are
staged for commit, avoiding accidental commits.
Xl. NOTE:
1. Tell them about GitHub Student Developer Pack
# Clone a repository
git clone REPOSITORY_URL
# Check the state of the working directory and the staging are
git status
# Rename a file
git mv OLD_NAME NEW_NAME
# Delete a branch
git branch -d BRANCH_NAME
# What comes when I make a new repo for importing from a local
git remote add origin https://github.com/Rush-Code10/name_of_
git branch -M main
git push -u origin main
# A few commands
git config --list (u can see all the configurations)
after git log to come out use the key q
git init
This initializes a new Git repository in the current directory.
mkdir my_project
cd my_project
git init
git add .
git mv
Rename or move a file:
git restore
Restore a file that you modified but don’t want to include anymore:
git rm
Remove a file from the working directory and staging area:
git rm file.txt
git diff
Compare changes between the working tree and the last commit:
git diff
git grep
Search for a specific string in the repository:
git log
View the history of commits:
git log
git show
Show details of a specific commit or object:
git status
Check the current state of the working directory and staging area:
git status
Delete a branch:
git commit
Record changes to the repository:
git merge
Merge changes from one branch into the current branch:
git rebase
Reapply commits from your current branch onto another branch:
git reset
Move the current HEAD to a different commit:
git switch
Switch between branches:
git tag
5. Collaborate
git fetch
Fetch updates from the remote repository without merging:
git pull
Fetch and merge updates from a remote branch into your current branch:
git push
Push local commits to the remote repository: