Module 4
4.4: Git & Github
Module 4
4.4: Git & Github
The git log command shows you a history of all the commits made in your Git repository. This is helpful for tracking changes, seeing who made which changes, and understanding the evolution of the project.
How to Use git log:
Open Git Bash.
Navigate to your project directory (if not already there).
Run the following command:
git log
Explanation:
git log will display a list of all commits in your repository.
Each commit will show:
Commit hash: A unique identifier for each commit.
Author: Who made the commit.
Date: When the commit was made.
Commit message: The description of the changes made.
Example Output:
commit d4eae3adfd8b28bb87f54746a2b388bc30f1e1df
Author: Your Name <your.email@example.com>
Date: Sat Sep 25 10:14:48 2021 -0400
Added new heading to index.html
commit a7b8b980cd446a759f88cfb52fecc65a59b79a4c
Author: Your Name <your.email@example.com>
Date: Fri Sep 24 14:10:17 2021 -0400
Updated footer section
When you create a Git repository locally, you might want to push your changes to a remote server, like GitHub. To do this, you need to first link your local repository to a remote repository using the git remote add command.
How to use git remote add origin <url>:
Create a new repository on GitHub (or any other Git hosting service).
Open Git Bash or your terminal.
Navigate to your local repository (the folder you’ve been working in).
Run the following command:
git remote add origin <url-of-repo>
Explanation:
origin is the default name for your remote repository.
<url-of-repo> is the URL of your GitHub repository (you can find it on your repository page, typically under "Clone or download").
Example:
git remote add origin https://github.com/yourusername/your-repo.git
This links your local repository to the remote one on GitHub.
Once your local repository is linked to the remote, you can push your changes to GitHub.
How to use git push origin master:
After committing your changes (using git commit), you can push the changes to the remote repository.
Run the following command
git push origin master
Explanation:
origin: The name of the remote repository.
master: The branch you're pushing to (if you're using the default branch, this is usually master).
What happens:
Your local changes are uploaded to GitHub and become visible to everyone who has access to the remote repository.
If you want to copy a repository from GitHub to your local machine, you can use the git clone command.
How to use git clone <url>:
Go to the GitHub repository page.
Copy the URL of the repository (it will look something like https://github.com/yourusername/your-repo.git).
Open Git Bash or your terminal.
Run the following command:
git clone <url-of-repo>
Explanation:
git clone: Downloads the entire repository from GitHub to your local machine, along with its history and all branches.
Example:
git clone https://github.com/yourusername/your-repo.git
This command creates a local copy of the repository on your machine.
If others have made changes to the remote repository and you want to bring those changes to your local machine, use the git pull command.
How to use git pull origin master:
Open Git Bash.
Navigate to your local repository.
Run the following command:
git pull origin master
Explanation:
git pull: Fetches the changes from the remote repository.
origin: The name of your remote repository.
master: The branch from which you want to pull changes (usually master).
What happens:
This command downloads the latest changes from the remote repository and merges them with your local branch.
To see all the branches in your repository (both local and remote), you can use the git branch command.
How to use git branch:
Open Git Bash.
Navigate to your local repository.
Run the following command:
git branch
Explanation:
This command lists all branches in your local repository.
It will show a * next to the currently active branch.
Example Output:
* master
feature-xyz
You can create new branches in your Git repository to work on new features or fixes without affecting the main branch.
How to create a new branch:
Open Git Bash.
Navigate to your repository.
Run the following command:
git branch <name>
Explanation:
git branch: This creates a new branch in your local repository.
<name>: Replace this with the name of your new branch.
Example:
git branch feature-xyz
This creates a new branch called feature-xyz.
Once you’ve created a new branch, you need to switch to it to start working on it. This is where git checkout comes in.
How to switch branches:
Open Git Bash.
Navigate to your repository.
Run the following command:
git checkout <branch-name>
Explanation:
git checkout: This switches you to the branch you specify.
<branch-name>: Replace this with the name of the branch you want to switch to.
Example:
git checkout feature-xyz
Now, you're on the feature-xyz branch and can start working on your changes.
When you’re done with a branch and no longer need it, you can delete it using the git branch -D command.
How to delete a branch:
Open Git Bash.
Navigate to your repository.
Run the following command:
git branch -D <name>
Explanation:
git branch -D: This deletes the specified branch, but only if it’s no longer needed or merged.
<name>: Replace this with the name of the branch you want to delete.
Example:
git branch -D feature-xyz
This deletes the feature-xyz branch locally.