0% found this document useful (0 votes)
4 views6 pages

VesionControl Intermediate

The document provides comprehensive tutorials on various Git concepts including branching, merging, resolving conflicts, using tags, reverting changes, pull requests, remotes, cherry-picking commits, stashing changes, Git hooks, and best practices for commit messages. Each section outlines essential commands and procedures for effective collaboration and version control in software development. The content is aimed at helping users understand and utilize Git's functionalities to manage their projects efficiently.

Uploaded by

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

VesionControl Intermediate

The document provides comprehensive tutorials on various Git concepts including branching, merging, resolving conflicts, using tags, reverting changes, pull requests, remotes, cherry-picking commits, stashing changes, Git hooks, and best practices for commit messages. Each section outlines essential commands and procedures for effective collaboration and version control in software development. The content is aimed at helping users understand and utilize Git's functionalities to manage their projects efficiently.

Uploaded by

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

Intermediate Print

Understanding branching and merging concepts Tutorial


Understanding branching and merging concepts is essential for effective collaboration in Git. Branching
allows you to create separate lines of development, enabling multiple features or fixes to be worked on
simultaneously without interfering with the main codebase. Merging integrates the changes from
different branches back into the main line, ensuring that all contributions are combined while
maintaining the integrity of the project.
Copy
Creating a Branch and Switching to It
git branch feature-branch
git checkout feature-branch

Merging a Branch into Main


git checkout main
git merge feature-branch

Viewing Branches
git branch

Deleting a Branch
git branch -d feature-branch

Resolving a Merge Conflict


// Open the conflicted file in a text editor and make the necessary adjustments.
git add conflicted-file
git commit

Resolving merge conflicts Tutorial


Resolving merge conflicts occurs when multiple changes are made to the same line of a file or when a
file is deleted in one branch and modified in another. Git will alert you to these conflicts during a merge,
allowing you to manually review the issues and decide how to integrate the changes. This process
generally involves understanding which changes to keep, editing the conflicting files to resolve the
discrepancies, and then committing the resolved changes back to the repository.
Copy
Example 1: Identifying a Merge Conflict
git merge feature-branch

Example 2: Manual Conflict Resolution in a Text Editor


// Conflicted file displayed in editor:
// <<<<<<< HEAD
// Your changes here
// =======
// Incoming changes from feature-branch
// >>>>>>> feature-branch
Example 3: Using Git Commands to Mark as Resolved
git add <filename>
git commit -m 'Resolved merge conflict in <filename>'

Using tags in Git Tutorial


Using tags in Git allows you to create specific points in your repository's history that are important, such
as releases or other significant milestones. Tags can be lightweight or annotated, and they serve as a
convenient way to mark versions of your project that can be easily referenced or checked out later.
Copy
Creating a lightweight tag
git tag v1.0

Creating an annotated tag


git tag -a v1.0 -m 'Version 1.0 release'

Listing all tags


git tag

Viewing tag details


git show v1.0

Pushing tags to remote repository


git push origin v1.0

Pushing all tags to remote repository


git push --tags

Reverting changes with Git Tutorial


Reverting changes with Git allows you to undo changes made in your repository's history. This can be
particularly useful when you need to remove a faulty commit or restore a previous state of the project.
Git provides various methods for reverting changes, including using commands to create new commits
that undo previous changes, ensuring that your project's history remains intact while removing
unwanted modifications.
Copy
Revert a specific commit
git revert <commit-hash>

Revert the last commit


git revert HEAD

Revert multiple commits


git revert <commit-hash-1>..<commit-hash-2>

Revert a file to a specific commit


git checkout <commit-hash> -- <file-path>

Introduction to pull requests Tutorial


A pull request (PR) is a method of submitting contributions to a project. It enables developers to notify
team members that they have completed a feature or bug fix and that it is ready for review. Pull
requests facilitate code review, discussion, and can include additional features such as merging
changes into the main codebase.
Copy
Creating a Pull Request on GitHub
1. Push your feature branch to the remote repository.
2. Go to the GitHub repository in your browser.
3. Select the 'Pull requests' tab.
4. Click on 'New pull request'.
5. Choose your feature branch and the base branch (usually main).
6. Add a title and description, then click 'Create pull request'.

Reviewing a Pull Request


1. Go to the 'Pull requests' tab in the repository.
2. Click on the pull request to review.
3. Use the 'Files changed' tab to see changes.
4. Add comments and suggestions if needed.
5. Click 'Approve' or 'Request changes' as needed.

Merging a Pull Request


1. After review, go to the pull request page.
2. Click on the 'Merge pull request' button.
3. Confirm the merge by clicking on 'Confirm merge'.
4. Optionally delete the feature branch after merging.

advertisement
Using remotes in Git Tutorial
Using remotes in Git refers to the ability to interact with remote repositories, which are hosted on
servers or cloud platforms. This allows developers to collaborate on projects, share their code, and
integrate changes made by others. Remotes are simply pointers to repositories in a different location,
and common operations include adding a remote, fetching changes from a remote, pushing local
commits to remote branches, and pulling updates from remotes to synchronize local work with the
remote repository.
Copy
Adding a remote repository
git remote add origin https://github.com/user/repo.git

Fetching changes from a remote


git fetch origin

Pushing local changes to a remote branch


git push origin main

Pulling changes from a remote branch


git pull origin main

Listing all remotes


git remote -v
Cherry-picking commits Tutorial
Cherry-picking commits in Git allows you to select specific commits from one branch and apply them to
another branch. This is particularly useful when you want to incorporate certain changes without
merging the entire branch, allowing for more granular control over what changes are introduced to your
codebase.
Copy
Cherry-pick a single commit
git cherry-pick <commit-hash>

Cherry-pick a range of commits


git cherry-pick <start-commit-hash>^..<end-commit-hash>

Cherry-pick with creating a new commit


git cherry-pick -n <commit-hash>

Abort a cherry-pick
git cherry-pick --abort

Skip a commit during cherry-picking due to conflict


git cherry-pick --skip

Stashing changes in Git Tutorial


Stashing changes in Git allows you to temporarily save modifications that you have made to your
working directory but do not want to commit yet. This is useful when you want to switch branches or
pull new changes from a remote repository without committing unfinished work. The stashed changes
can be reapplied later when you're ready to continue working on them.
Copy
Stash Changes
git stash

List Stashed Changes


git stash list

Apply Stashed Changes


git stash apply

Apply Specific Stash


git stash apply stash@{0}

Pop Stashed Changes


git stash pop

Drop a Stash
git stash drop stash@{0}

Clear All Stashes


git stash clear

Using Git hooks Tutorial


Git hooks are scripts that Git executes before or after events such as commits, pushes, and merges.
These hooks allow developers to automate tasks, enforce coding standards, run tests, or notify relevant
parties when certain actions occur. They are defined in the hooks directory of a Git repository and can
be written in various scripting languages, such as Bash or Python.
Copy
Pre-commit Hook Example
#!/bin/sh
# This hook checks for linting issues before committing
npm run lint

Post-commit Hook Example


#!/bin/sh
# This hook sends a notification to a server after a commit
curl -X POST http://myserver.com/notify --data 'Commit made on $(date)'

Pre-push Hook Example


#!/bin/sh
# Prevent pushing to master branch without tests
if [[ $1 == 'refs/heads/master' ]]; then
echo 'Push to master is blocked. Please run tests first.'
exit 1
fi

Best practices for commit messages Tutorial


Best practices for commit messages help maintain a clear and concise project history, making it easier
for contributors to understand the purpose of changes, facilitate communication, and enhance
collaboration within the development team. Good commit messages should be structured, consistent,
and provide context about the changes made, following conventions that promote clarity.
Copy
Example of a comprehensive commit message
fix: correct typo in README

This commit fixes a typo in the installation instructions section of the README file. This
typo may have caused confusion for new users.

Example of a commit message with user story reference


feat: add user login feature

Implemented the user login functionality as part of the user story #345 to enhance user
authentication and security.

Example of a single-line commit message


style: update styles for the header component

Example of a commit message with issue tracking


fix: resolve issue #123 regarding slow loading times of the homepage

Example of a commit message using imperative mood


docs: update API documentation for better clarity

You might also like