What is GitHub?
GitHub is a platform where developers store and share their code. Think of it like a social network for programmers, but instead of sharing photos or updates, they share software projects. It helps developers collaborate on projects, track changes, and manage different versions of their code. It’s widely used for open-source projects and by teams working on software development together.
1. Installing and Getting Started with Git in Linux
1 | sudo apt update && sudo apt upgrade -y |
1.2. Configuring Git
After installing Git, you should configure it with your name and email. This information is used to identify your commits.
Set Your Name: Replace
Your Name
with your actual name:1
git config --global user.name "Your Name"
Set Your Email: Replace
your.email@example.com
with your email address:1
git config --global user.email "your.email@example.com"
1.3. Cloning an Existing Repository:
Navigate to the Directory: Open Terminal and navigate to the directory where you want to clone the repository. For example:
1
cd /path/to/your/desired/directory
Clone the Repository: Use the
git clone
command followed by the URL of the GitHub repository you want to clone. For example, to clone a repository namedexample_repo
from GitHub:1
git clone https://github.com/username/example_repo.git
Replace
username
with the GitHub username andexample_repo
with the name of the repository you want to clone.Navigate into the Cloned Repository: After cloning, navigate into the cloned repository:
1
cd example_repo
2. Branching in the repositories
2.1. Creating a new branch
1 | git branch --all |
2.2. Deleting a branch
Once you’ve merged your changes from a branch and no longer need it, you can delete it:
Delete Locally:
1
git branch -d branch-name
This deletes the branch named
branch-name
locally, but only if its changes have been merged into another branch.Delete Remotely: If you’ve pushed the branch to GitHub and want to delete it there too:
1
git push origin --delete branch-name
This deletes the branch named
branch-name
on the remote repository (origin
on GitHub).
2.3 Add Files
Start tracking files by adding them to the staging area:
1 | git add <file> # Replace <file> with the actual filename or use '.' to add all files |
2.4 Commit Changes
Commit the staged changes to the repository:
1 | git commit -m "Initial commit" # Use a meaningful commit message |
3. Merging the branches
Navigate to main: First, ensure you are on the branch into which you want to merge changes. Typically, this is the main
branch:
1 | git checkout main |
Fetch Latest Changes: It’s a good practice to fetch the latest changes from the remote repository before merging:
1 | git fetch origin |
Merge Branch: Merge the changes from the branch you want to merge (let’s say devpt
) into main
:
1 | git merge devpt |
4. Creating patches
List all commits on a specific branch: Replace <branch-name>
with the name of the branch you’re interested in, such as devpt
in your case.
1 | git log devpt (or) |
Generate patches for the commits:
1 | git format-patch <commit_hash> (or) # for a particular |
4.1 Applying the patch files
If you have .patch
files generated using git format-patch
or another method, you can apply them using git apply
or git am
.
4.1.2 Using git apply
Navigate to your Git repository:
1
cd /path/to/your/repository
Apply the patch using
git apply
:1
git apply path/to/your/patchfile.patch
Replace
path/to/your/patchfile.patch
with the actual path to your patch file.
5. Reset the repo to the specific commit
Use git reset
to move the HEAD
pointer and working directory to the specific commit you want to start from. This effectively “rewinds” your repository’s state to that commit.
1 | git log devpt |
Replace <commit-hash>
with the hash of the commit you want to start from. This hash uniquely identifies each commit and can be found using git log
.