Introduction to Git & GitHub
Git​
Git is a version control system that allows you to track changes to your code, collaborate with others, and manage your codebase. It helps you keep a record of who made changes, when they were made, and why they were made.
GitHub​
GitHub is a web-based hosting platform designed for developers. It provides a centralized location to store your code repositories and enables collaboration among team members. GitHub supports projects in various programming languages and allows others to review and contribute to your code. It is a free and open-source platform, making it widely used and popular among developers.
Steps to Create an Account on GitHub​
- Open the GitHub website by visiting Click Here to Open GitHub Website
- Click on the Sign up button.
- Enter your email address.
- Create a strong and secure password.
- Choose a unique username for your GitHub account.
6.Click the Create an account button.
- Complete the CAPTCHA puzzle to verify that you are a human.
- Check your email for a message from GitHub and click the Verify email address button.
Repository (Repo)​
In GitHub, a repository, or "repo" for short, is a central location where you can store your source code and related files for a specific project.
Steps to Create Repository​
- On the GitHub page, click on the + drop-down menu in the upper-right corner and select New repository.
- Enter a name for your repository, for example, sample.
- Choose the visibility of your repository (private or public).
It is recommended to keep your repository public.
- Click the Create repository button to create your repository.
- You will see an empty repository page.
- Open your Git Bash (command line) and run the following command, replacing
<repository-link>
with the link available on your repository:
git clone <repository-link>
Replace <repository-link>
with the actual link available on your repository
- You will see a new folder named sample on your desktop.
Account Setup Credentials​
To set up your account credentials for Git, follow these steps:
Open a terminal or command prompt.
Set your username globally using the command:
git config --global user.name "Your Username"
Set your email globally using the command:
git config --global user.email "[email protected]"
Git Commands​
git init
The git init
command is used to initialize a new Git repository in your project folder. This command is used once during the initial setup of a new repository.
git status
The git status
command displays the status of your working project. It shows which files have been modified or staged and which files are untracked.
git add <filename>
The git add <filename>
command adds new or changed files in your working project to the Git staging area. You can add specific files by providing their filenames, or you can use git add .
to add all changed files.
git commit
The git commit
command is used to record the changes in the repository. Each commit includes a commit message that describes the changes made in that commit.
Syntax :
git commit -m "commit message"
git push
The git push
command is used to upload local branch commits to GitHub. It pushes your local changes to the remote repository on GitHub.
To push your code to GitHub using Git commands, follow these steps:
Pushing Existing Code to a Git Repository​
- Initialize a Git repository: If you haven't already done so, navigate to the root directory of your project using a command-line interface, and run the following command to initialize a Git repository:
git init
Create a remote repository: Create a new repository on a Git hosting platform such as GitHub Follow the platform's instructions to set it up.
Add a remote repository: Link your local repository with the remote repository using the following command:
git remote add origin <remote_repository_url>
Replace <remote_repository_url> with the URL of the remote repository you created.
- Add files to the staging area: Use the following command to add the existing code files to the staging area, which prepares them for committing:
git add .
- Commit the changes: Committing creates a new snapshot of the code in the Git history. Run the following command to commit the changes:
git commit -m "Initial commit"
- Push the code: Finally, push your code to the remote repository using the following command:
git push
If you're using a branch other than master
, replace master
with the name of the branch.
Congratulations! You have successfully pushed your existing code to a Git repository.
Pushing New Code Directly to a Git Repository​
- Clone the repository: Start by cloning the existing repository to your local machine. Use the following command to clone the repository:
git clone <repository_url>
Replace <repository_url> with the URL of the repository you want to clone.
Create or modify files: Create new code files or modify existing ones using your preferred code editor or IDE.
Add files to the staging area: Use the git add command to add the new or modified files to the staging area:
git add .
- Commit the changes: Commit the changes by running the following command:
git commit -m "Commit message"
Replace Commit message with a descriptive message summarizing the changes made.
- Push the code: Finally, push the new code to the remote repository using the following command:
git push
Great job! You have now successfully pushed your new code directly to the Git repository.