0% found this document useful (0 votes)
8 views5 pages

Devops Lab Exp-2

The document outlines essential Git and GitHub commands for version control and collaboration in software development. Key commands include initializing a repository, checking status, adding files, committing changes, merging branches, and creating pull requests. It serves as a guide for users to effectively manage their code and collaborate with others using these tools.

Uploaded by

subbu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Devops Lab Exp-2

The document outlines essential Git and GitHub commands for version control and collaboration in software development. Key commands include initializing a repository, checking status, adding files, committing changes, merging branches, and creating pull requests. It serves as a guide for users to effectively manage their code and collaborate with others using these tools.

Uploaded by

subbu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO: 2.

Explore Git and GitHub commands Aim: Explore


Git and GitHub commands
DESCRIPTION:
Git and GitHub are two of the most popular tools used for version control and collaboration in
software development.
Here are some common Git and GitHub commands:

1. Initializing a Git repository: $ git init


To create a new repo, you'll use the git init command. git init is a one-time command
you use during the initial setup of a new repo. Executing this command will create a
new . git subdirectory in your current working directory.

2. Checking the status of your repository: $ git status


To start a repository, use either git init or git clone – not both. To initialize a
repository, Git creates a hidden directory called . git . That directory stores all of the
objects and refs that Git uses and creates as a part of your project's history.

3. Adding files to the stage: $ git add <file-name>


'git add' command is used to stage files. To stage all files use 'git add . ', to stage a
specific file use 'git add [filename]', and to stage a folder use 'git add [folderpath]'.

4. Committing changes: $ git commit -m "commit


message"
Step 1: Identify the commit to be amended. Use the following command to view the
commit history and identify the commit message you want to edit: git log. ...
Step 2: Edit the commit message. ...
Step 3: Save the changes. ...
Step 4: Push the amended commit.

5. Checking the commit history: $ git log


Each branch has a commit history. To list commits as a view of a branch's history,
you can use the git log command with the branch name. git log : shows the commit
history for the branch currently checked out. If you have not checked out a branch,
this will show you the commit history of the entire repository.

6. Undoing changes: $ git checkout <file-name>


Undo local changes
Confirm that the file is unstaged (that you did not use git add <file> ) by running git
status : Shell. git status. Example output: Shell. ...
Choose an option and undo your changes: To overwrite local changes: Shell. git
checkout -- <file> To discard local changes to all files, permanently:
7. Creating a new branch: $ git branch <branch-name>
How to Create a New Branch in Git and Push the Code?
1. git branch <Branch name>
2. git checkout -b <new_branch_name>
3. git branch <branch_name>
4. git checkout <branch_name>
5. git push <remote_name> <branch_name>
6. git checkout <branch_name>
7. git checkout <commit-hash> <file-name>
8. git branch.

8. Switching to a different branch: $ git checkout <branch-name>


git checkout
a) The "checkout" command can switch the currently active branch - but it can also be
used to restore files.
b) The name of a local branch that you want to switch to. ...
c) Creates a new local branch and directly switches to it. ...
d) Creates a new local branch - and sets up an "upstream" configuration.

9. Merging two branches: $ git merge <branch-name>


The command git merge <branch-name> integrates changes from the specified branch into
the currently active branch. This is a fundamental operation in Git for combining work done
in different branches.
Here's a breakdown:

1. Purpose:

 To combine changes from one branch into another.


 Often used to integrate feature branches into a main branch (e.g., main or master).

2. How it works:

 You must be on the branch into which you want to merge the changes. For example, if
you want to merge feature-branch into main, you must first switch to the main branch
using git checkout main.
 Then, use the command git merge feature-branch.
 Git will try to automatically merge the changes.
3. Types of Merges:

Fast-forward merge:
If the target branch has no new commits since the source branch was created, Git simply
moves the target branch pointer to the latest commit of the source branch.
Three-way merge:
If the target branch has new commits, Git creates a new merge commit that combines the
changes from both branches. This commit has two parent commits: the latest commit of
the target branch and the latest commit of the source branch.

4. Merge Conflicts:
If the same parts of the same files have been modified differently on both branches, Git
cannot automatically merge them.
You will need to manually edit the conflicted files, remove the conflict markers, and then
add and commit the changes.

10. Pushing changes to a remote repository: $ git push origin <branch-name>


What is Git Push?
After the user is done with the modifications in the Local repository, there is a
need to push these changes to the remote repository. This can be done with the git
push command. Users can define which branch is to be pushed into the repository
by passing its name as an argument. By default, the data will be pushed from the
current branch into the same branch of the remote repository.

Push Commits To a Git Repository


By following the below steps we can push the commit to the git repository.
Step 1: Make sure that your local and Git repositories are up.
Step 2: Stage the modified files using the command line below.
git add .
(.) represents all the untracked files. If you want to move a specific file then you can the
following command.
git add <name of the file>
Step 3: Commit the staged files into the local repository using the following command.
Provide a commit message that details the changes you made and is descriptive.
git commit -m "message"
Step 4: Push the commit to the remote repository from there other developers can access
the code. Use the following command.
git push <Renote URL>
In the event that conflicts arise during the push, resolve them manually before going
through steps 2 and 4 again to push the modifications.
Note: You must have the required access and permissions before you can push your
commits to a Git repository.

11. Cloning a repository from GitHub: $ git clone <repository-url>


To clone a repository:
a) Open a terminal (or Git Bash on Windows).
b) Run the command: git clone <repository-url>
c) Replace <repository-url> with the URL of the Git repository you wish to clone. This
URL can be found on the repository's page on GitHub or any Git hosting service.

12. Creating a pull request on GitHub: Go to the repository on GitHub, select the branch
you want to merge and click the "New pull request" button.

To create a pull request on GitHub, navigate to the repository, click "Compare & pull
request," select the target branch and the branch with your changes, add a title and
description, and then click "Create Pull Request" or "Create Draft Pull Request" according to
GitHub Docs.
Here's a more detailed breakdown:
1. Navigate to the Repository: Go to the repository's main page on GitHub.
2. Initiate the Pull Request: Look for a yellow banner above the file list and click on
"Compare & pull request".

3. Select Branches:

 Use the "base" dropdown to choose the branch you want to merge into (e.g., main).
 Use the "compare" dropdown to select the branch containing your changes.
4. Add Details:

 Provide a title and a description for your pull request.


 The description should explain the changes you've made.
5. Create the Pull Request:
 Click "Create Pull Request" to create a pull request that's ready for review.
 Alternatively, click "Create Draft Pull Request" if you're not yet ready for review.

Here are some common Git and GitHub commands:

You might also like