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

Git_Intermediate

The document provides a comprehensive overview of various Git commands, including 'git merge', 'git rebase', 'git stash', 'git remote', 'git fetch', 'git cherry-pick', 'git tag', 'git reset', 'git diff', and 'git fetch --prune'. Each command is explained with its purpose, syntax, and examples, highlighting how they can be used to manage branches, commits, and remote repositories effectively. This serves as a useful tutorial for developers looking to enhance their Git proficiency.

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 views5 pages

Git_Intermediate

The document provides a comprehensive overview of various Git commands, including 'git merge', 'git rebase', 'git stash', 'git remote', 'git fetch', 'git cherry-pick', 'git tag', 'git reset', 'git diff', and 'git fetch --prune'. Each command is explained with its purpose, syntax, and examples, highlighting how they can be used to manage branches, commits, and remote repositories effectively. This serves as a useful tutorial for developers looking to enhance their Git proficiency.

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/ 5

Intermediate Print

git merge <branch> - Merge specified branch into the current branch.
Tutorial
The 'git merge <branch>' command is used to integrate changes from the specified branch into the
current branch. This operation combines the histories of both branches, allowing you to bring in
updates, features, or bug fixes made on the specified branch while preserving the history of commits.
Copy
Merge feature branch
git merge feature-branch

Merge with a specific message


git merge feature-branch -m 'Merging feature branch into main'

Merge and resolve conflicts


git merge feature-branch
# Resolve any conflicts, then use:
git add <resolved_file>
git commit

git rebase <branch> - Reapply commits from one branch onto another.
Tutorial
The 'git rebase <branch>' command is used to move or combine a sequence of commits to a new base
commit, which allows for a cleaner project history by incorporating changes from one branch onto
another. It's particularly useful when you want to update a feature branch with the latest updates from
the main branch before merging.
Copy
Example 1: Rebase a feature branch onto the main branch
git checkout feature-branch
git rebase main

Example 2: Rebase interactively to edit, squash or reorder commits


git rebase -i HEAD~5

Example 3: Skip commits that are already applied during rebasing


git rebase --skip

Example 4: Abort a rebase operation if conflicts occur


git rebase --abort

git stash - Stash changes in a dirty working directory. Tutorial


The 'git stash' command allows developers to temporarily save changes in a dirty working directory
without committing them to the current branch. This is particularly useful when you need to switch
branches or pull in changes from a remote repository but aren't ready to commit your current work. The
stashed changes can be reapplied later using 'git stash apply' or 'git stash pop'.
Copy
Stash your changes
git stash

Stash changes with a message


git stash save 'message describing the stash'

List all stashes


git stash list

Apply the most recent stash


git stash apply

Pop the most recent stash


git stash pop

Drop a specific stash


git stash drop stash@{0}

git remote - Manage set of tracked repositories. Tutorial


The 'git remote' command is used to manage the set of tracked repositories in Git. It allows developers
to add, remove, and update references to remote repositories, making it easier to collaborate with
others and manage codebases that reside in different locations.
Copy
Add a remote repository
git remote add <name> <url>

View remote repositories


git remote -v

Remove a remote repository


git remote remove <name>

Rename a remote repository


git remote rename <old-name> <new-name>

Fetch changes from a remote repository


git fetch <name>

git fetch - Download objects and refs from another repository. Tutorial
The 'git fetch' command downloads the latest changes from a remote repository without merging them
into the current branch. It updates the local copies of remote branches, allowing users to review
changes before integrating them into their local codebase.
Copy
Fetch from default remote repository
git fetch

Fetch from a specific remote repository


git fetch origin

Fetch changes from a specific branch


git fetch origin main

Fetch and prune deleted branches


git fetch --prune

advertisement
git cherry-pick <commit> - Apply the changes introduced by an existing
commit. Tutorial
The 'git cherry-pick <commit>' command allows you to select specific commits from one branch and
apply them to another branch. This is useful when you want to incorporate certain changes from a
different branch without merging all the changes from that branch. Cherry-picking helps maintain a
cleaner commit history by allowing targeted integration of changes.
Copy
Cherry-pick a single commit
git cherry-pick abc1234

Cherry-pick multiple commits


git cherry-pick abc1234 def5678

Cherry-pick a commit with a range


git cherry-pick abc1234^..def5678

Cherry-pick and resolve conflicts


git cherry-pick abc1234
# Resolve any conflicts
git add <resolved files>
git cherry-pick --continue

git tag - Create, list, or delete tags. Tutorial


Tags in Git are used to mark specific points in the repository's history as important. They are commonly
used to mark release points (e.g., v1.0, v2.0). Unlike branches, tags do not change and are meant to be
immutable references to specific commits.
Copy
Create a tag
git tag v1.0

List all tags


git tag

Delete a tag
git tag -d v1.0

Create a tag with a message


git tag -a v1.0 -m 'Release version 1.0'

Push tags to the remote repository


git push origin --tags

git reset <file> - Unstage a file while retaining changes. Tutorial


The 'git reset <file>' command is used to unstage a file that has been added to the staging area while
preserving the changes made to that file in the working directory. This allows the developer to keep
their edits without including them in the next commit, providing flexibility in managing which changes to
track.
Copy
Unstage a specific file
git reset myfile.txt

Unstage multiple files


git reset file1.txt file2.txt

Unstage all files


git reset .

git diff - Show changes between commits, commit and working tree, etc.
Tutorial
The 'git diff' command is used to show the differences between various states of your repository. It can
be applied to compare changes between commits, between the working tree and index, or between
your current working directory and the last commit. This helps developers understand what
modifications have been made, review changes before committing, or assess differences between
branches.
Copy
Show changes in the working directory
git diff

Show changes staged for the next commit


git diff --cached

Show changes between two commits


git diff <commit1> <commit2>

Show changes between a branch and the working tree


git diff <branchname>

Show changes between the working directory and the last commit
git diff HEAD

git fetch --prune - Remove remote-tracking references that no longer exist on


the remote. Tutorial
The 'git fetch --prune' command is used to update the local repository with changes from the remote
repository while also cleaning up any remote-tracking references that no longer exist on the remote.
This is particularly useful when branches have been deleted on the remote, as it helps keep your local
repository tidy by removing references to those branches.
Copy
Fetch and Prune
git fetch --prune

Fetch from a Specific Remote with Pruning


git fetch origin --prune

You might also like