A curated list of essential Git commands and resources to help you learn Git faster.
git init
git clone
git add
git commit
git status
git diff
git checkout
git reset
git log
git show
git tag
git push
git pull
git branch
git checkout -b
git merge
git rebase
git branch --set-upstream-to
git branch --unset-upstream
git cherry-pick
git merge
git rebase
git stash
git stash pop
git stash list
git stash apply
git stash drop
git remote
git remote add
git remote remove
git fetch
git pull
git push
git clone --mirror
git config
git global config
git reset config
git cat-file
git checkout-index
git commit-tree
git diff-tree
git for-each-ref
git hash-object
git ls-files
git ls-remote
git merge-tree
git read-tree
git rev-parse
git show-branch
git show-ref
git symbolic-ref
git tag --list
git update-ref
git blame
git bisect
git checkout
git commit
git diff
git fetch
git grep
git log
git merge
git push
git rebase
git reset
git show
git tag
git config --global alias.<alias> <command>
git config --local core.hooksPath <path>
git annex
git am
git cherry-pick --upstream
git describe
git format-patch
git fsck
git gc
git help
git log --merges
git log --oneline
git log --pretty=
git log --short-commit
git log --stat
git log --topo-order
git merge-ours
git merge-recursive
git merge-subtree
git mergetool
git mktag
git mv
git patch-id
git p4
git prune
git pull --rebase
git push --mirror
git push --tags
git reflog
git replace
git reset --hard
git reset --mixed
git revert
git rm
git show-branch
git show-ref
git show-ref --heads
git show-ref --tags
git stash save
git subtree
git tag --delete
git tag --force
git tag --sign
git tag -f
git tag -l
git tag --verify
git unpack-file
git update-index
git verify-pack
git worktree
- Git Official Documentation
- GitHub Learning Lab
- Codecademy Course
- Pro Git: by Scott Chacon [Book]
- FreeCodeCampOrg- Beginner
- FreeCodeCampOrg- Intermediate
- Programming with Mosh
git pull
and git fetch
are both Git commands used to retrieve changes from a remote repository, but they differ in how they integrate those changes into the local repository. Let's explore the differences with examples:
-
Purpose: Fetches changes from a remote repository to the local repository, but it does not automatically merge or rebase them into the working branch.
-
Usage:
git fetch origin
-
Example Workflow:
# Fetch changes from the remote repository git fetch origin # View the changes fetched git log origin/master # Merge the fetched changes into the local branch (if desired) git merge origin/master
- Purpose: Fetches changes from a remote repository and automatically merges or rebases them into the working branch.
- Usage:
git pull origin master
- Example Workflow:
# Fetch and merge changes from the remote repository into the current branch git pull origin master
-
Automatic Integration:
git fetch
fetches changes but leaves them in the remote-tracking branches (e.g.,origin/master
), and you need to explicitly merge or rebase.git pull
fetches and integrates changes automatically, either by merging or rebasing.
-
Workflow:
- With
git fetch
, you have more control over how and when you integrate changes. git pull
is a shortcut that combinesgit fetch
and eithergit merge
orgit rebase
.
- With
-
Commit History:
git fetch
does not modify your local branches, preserving your commit history.git pull
, especially with rebase, can rewrite commit history as it integrates changes.
-
Safety:
git fetch
is safer in a collaborative environment, as it allows you to review changes before integration.git pull
may automatically merge changes, which could lead to conflicts that need to be resolved.
Suppose you're working on a branch named feature-branch
and want to update it with changes from the remote master
branch:
-
Using
git fetch
and merge:git fetch origin # Fetch changes git merge origin/master # Merge changes into your local branch
-
Using
git pull
:git pull origin master # Fetch and automatically merge changes
In summary, use git fetch
when you want to review changes before integrating them, and use git pull
when you want a quick way to fetch and integrate changes automatically. The choice depends on your workflow and the level of control you want over the integration process.
git merge
and git reset
are both Git commands that are used for different purposes in managing changes within a Git repository. Let's explore the differences between them with examples:
-
Purpose: Combines changes from different branches, integrating them into the current branch.
-
Usage:
git merge <branch-name>
-
Example Workflow:
# Switch to the branch where you want to merge changes git checkout target-branch # Merge changes from the source branch git merge source-branch
- Purpose: Resets the current branch to a specified commit, either preserving or discarding changes.
- Usage:
git reset <commit>
- Example Workflow (Discarding Changes):
# Reset the branch to a specific commit, discarding changes git reset --hard <commit>
- Example Workflow (Preserving Changes):
# Reset the branch to a specific commit, preserving changes as uncommitted git reset --soft <commit>
-
Operation:
git merge
combines changes from different branches, creating a new commit that reflects the integration of changes.git reset
moves the branch pointer to a specified commit, potentially discarding or preserving changes.
-
Commit History:
git merge
introduces a new commit to represent the integration of changes, preserving the commit history of both branches.git reset
modifies the commit history by moving the branch pointer, potentially discarding commits (with--hard
) or preserving them as uncommitted changes (with--soft
).
-
Branches:
git merge
is typically used to merge changes between branches.git reset
is often used for undoing commits or resetting the branch pointer.
Suppose you have a feature branch (feature-branch
) and you want to integrate its changes into the main branch (main
). You have two options:
-
Using
git merge
:git checkout main # Switch to the main branch git merge feature-branch # Merge changes from feature-branch into main
-
Using
git reset
to undo changes:git checkout main # Switch to the main branch git reset --hard feature-branch # Discard changes and move main pointer to feature-branch
In this case, the
--hard
option discards all changes, including those made in thefeature-branch
. Be cautious when using--hard
as it permanently discards changes.
Choose between git merge
and git reset
based on your desired outcome and the nature of the changes you want to integrate or undo.