Demo Project for Git and Git hub

Step 1: Install Git

Installing Git on your computer allows you to use Git commands to track changes in your project. You can download and install Git from the official website (https://git-scm.com/downloads).

Step 2: Set up Git

  • Configuring your name and email address in Git helps identify you as the author of the changes you make in your project. For example:

git config –global user.name “John Doe”
git config –global user.email “johndoe@example.com”

Step 3: Create a local Git repository

Initializing a Git repository in your project directory creates a hidden folder named “.git” that stores the version control information for your project. For example:

cd myproject/
git init

Step 4: Add files to the repository

By adding files to the Git repository, you tell Git to start tracking changes in those files. For example, to add all the files in the current directory, use the command:

git add .

Step 5: Commit changes

Committing changes means saving a snapshot of your project’s current state. You provide a descriptive message to explain what changes you made. For example:

git commit -m “Initial commit – added index.html and style.css files”

Step 6: Create a repository on GitHub

  • Creating a repository on GitHub provides a remote location where you can store your project and collaborate with others. Go to GitHub, sign in, and create a new repository with a name of your choice.

Step 7: Link local repository to GitHub repository

  • By linking your local Git repository to the GitHub repository, you establish a connection between them. Use the following command, replacing “yourusername” with your GitHub username and “yourrepository” with the repository name you created on GitHub:

git remote add origin https://github.com/yourusername/yourrepository.git

Step 8: Push changes to GitHub

  • Pushing changes uploads your local Git repository to the GitHub repository. Use the following command to push your changes:

git push -u origin master

You might be asked to enter your GitHub username and password. This command uploads your files and commits to the GitHub repository.

Step 9: Verify changes on GitHub

Visit the GitHub repository’s page and check if your files and commits are visible. This confirms that your changes have been successfully uploaded.

Step 10: Make changes and update repository

  • After pushing your initial changes, you can continue making changes to your project. To update your GitHub repository with the latest changes, use the following commands:

git add .
git commit -m “Updated files – added contact form”
git push origin master

These commands add your changes, create a new commit with a descriptive message, and push the changes to GitHub.

If you have already pushed a file to GitHub and made additional changes to it locally, you can push the updated version to GitHub by following these steps:

Step 1: Check the status of your local repository Open your terminal or command prompt and navigate to the directory of your project. Run the following command to check the status of your repository and see the changes you’ve made:

git status

Step 2: Add the modified file to the staging area To prepare the modified file for the next commit, use the git add command followed by the file name. For example, if you have modified a file named “index.html”, use the following command:

git add index.html

If you have multiple modified files, you can add them all at once by using a dot instead of specifying individual file names:

git add .

This command adds all modified files to the staging area.

Step 3: Commit the changes After adding the modified file(s) to the staging area, you need to create a new commit to record the changes. Use the following command:

git commit -m “Updated file with additional changes”

Replace the commit message with a descriptive message that reflects the changes you made.

Step 4: Push the changes to GitHub Once you have committed the changes locally, you can push them to your GitHub repository. Use the following command:

git push origin master

This command pushes the committed changes to the “master” branch of your remote repository on GitHub.

After executing the push command, your updated file(s) will be uploaded to GitHub, and the changes will be visible in your repository.

Note: If you are working with a branch other than “master”, replace “master” in the git push command with the name of your branch.

In Git, the “checkout” command is used to switch between different branches or restore files from previous commits. It allows you to navigate to a specific branch or commit, and even revert changes made to files in your project. Here are some common use cases and examples of how to use the “checkout” command:

Switching to a branch: To switch to an existing branch in your project, use the following command:

git checkout <branch_name>

For example, to switch to a branch named “feature/login”, run:

git checkout feature/login

Creating and switching to a new branch: To create a new branch and switch to it at the same time, use the “-b” option with the “checkout” command:

git checkout -b

For example, to create and switch to a new branch named “bugfix/issue123”, run:

git checkout -b bugfix/issue123

Discarding local changes in a file: If you have made changes to a file and want to revert it to its previous state (as in the last committed version), use the following command:

git checkout —

For example, to discard local changes in a file named “script.js”, run:

git checkout — script.js

Restoring a specific file from a previous commit: If you want to retrieve a specific file as it was in a previous commit, use the commit hash and file path with the “checkout” command. Run the following command:

git checkout <commit_hash> — <file_path>

For example, to restore a file named “styles.css” from a commit with the hash “abcdef1234”, run:

git checkout abcdef1234 — styles.css

Detached HEAD state: When you checkout to a specific commit rather than a branch, you enter a “detached HEAD” state. It means that any new commits you make won’t be associated with a branch. To switch back to a branch, use the following command:

git checkout <branch_name>

For example, to switch back to the “master” branch, run:

git checkout master

Project that involves branching, merging, checkout, reverting, and viewing version history using Git and GitHub.

Push the branch to GitHub Push the branch and its commits to GitHub using the following command:

git push origin feature/branching-demo

Merge the branch Switch back to the main branch (e.g., “master”) using the following command:

git checkout master

Then, merge the changes from the branch into the main branch using the following command:

git merge feature/branching-demo

Revert changes If you want to revert the changes made in a specific commit, find the commit hash using git log and use the following command to revert it:

git revert <commit_hash>

View version history To view the version history and see the commits made in the repository, use the following command

git log –oneline

If you want to go back to the previous branch or commit after using the git checkout command, you can use the git checkout - command. Here’s how you can go back:

Check the current branch or commit: Run the following command to see your current branch or commit:

git branch

or

git log –oneline

Go back to the previous branch or commit: Use the following command to go back to the previous branch or commit:

git checkout –

To undo a merge and revert the changes it introduced, you can use the git revert or git reset command. Here’s a step-by-step guide on how to unmerge after merging:

Check the commit history:

Run the following command to view the commit history:

git log –oneline

Identify the merge commit: Look for the commit that represents the merge you want to undo. It typically has two or more parent commits, indicating that it is a merge commit.

Option 1: Revert the merge commit: If you want to keep a record of the merge and create a new commit that undoes the changes introduced by the merge, you can use the git revert command. Execute the following command, replacing <merge_commit_hash> with the actual commit hash of the merge commit:

git revert -m 1

The -m 1 option specifies the main branch to be kept, and the merge commit’s changes will be reverted. Git will create a new commit that undoes the merge changes.

Option 2: Reset to before the merge: If you want to completely remove the merge commit and all subsequent commits, and you are certain that you don’t need any of the changes introduced by the merge, you can use the git reset command. Run the following command, replacing <merge_commit_hash> with the actual commit hash of the merge commit:

git reset –hard <merge_commit_hash>^

The ^ symbol indicates the parent commit of the merge commit. This command moves the branch pointer back to the specified commit, effectively discarding the merge commit and any commits that came after it.

Push the changes (if necessary): If you have already pushed the merge commit and want to update the remote repository, you may need to force push the changes using:

git push -f

To create a pull request on GitHub, follow these step-by-step instructions:

Step 1: Clone the repository: Open your terminal or command prompt and navigate to the directory where you want to clone the repository. Use the following command to clone the repository:

git clone <repository_URL>

Replace <repository_URL> with the URL of the Git repository you want to clone. For example:


git clone https://github.com/yourusername/yourrepository.git

Step 2: Navigate to the project directory: Change to the project directory using the cd command. For example:

cd yourrepository

Step 3: Pull the latest changes: Before pulling the latest changes, it’s recommended to ensure that you are on the correct branch. Use the following command to list the branches and check your current branch:

git branch

Once you have confirmed that you are on the correct branch, pull the latest changes from the remote repository using the following command:

git pull

Step 4: Resolve any conflicts (if necessary): If there are conflicts between the changes you made locally and the changes pulled from the remote repository, Git will prompt you to resolve those conflicts. You will need to open the conflicting files, resolve the conflicts manually, save the changes, and then commit the resolved files.

Step 5: Verify the updated project: After pulling the latest changes, you can verify that your project is up to date by reviewing the updated files and code.

That’s it! You have successfully pulled the latest changes from the Git repository to your local project. It’s a good practice to regularly pull updates from the repository to stay in sync with the latest changes made by other contributors.

Note: If you are working with a branch other than the main branch (e.g., “master”), you may need to specify the branch name when pulling. For example:

git pull origin yourbranchname

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x