- Elementary commands#
git clone <url>
performs the following operations:Copy the remote repository
Create tracking branches for all remote branches.
Check out the active branch from the remote repository.
Note
git clone
is run typically only once
git fetch
without further arguments runs:Updates all remote tracking branches (“fetches the changes”).
Note
Running git fetch
is always a save operation
In short, git merge
is used to combine the changes from one branch into another branch.
git merge
procedure:Starting from the following setup:
C'-----D' main
/
A---B---C---D---E origin/main
A git merge
will perform:
Create a new commit on top of the current branch that contains all the changes from the commits from origin/main
main ∨ C'-----D'---F / / A---B---C---D---E origin/main
Set the head of your local branch to this new commit
C'-----D'---F main / / A---B---C---D---E origin/main
Note
If your current branch is main, a git merge
will be equivalent to git merge origin/main
.
In short, git rebase
is used to apply the changes from one branch onto another branch.
It differs from git merge
in that it rewrites the commit history of the current branch.
Meaning that the commits from the current branch are applied on top of the other branch.
git rebase
procedure:Starting from the following setup:
C'-----D' main
/
A---B---C---D---E origin/main
A git rebase
will perform:
Apply each commit from your local branch onto the remote branch:
C''---D'' main / A---B---C---D---E origin/main
Note
You might have to resovle conflicts for each commit from the local branch (i.e.
C'
andD'
).With
git rebase -i
you can also rewrite the history of a single branch, see the Rewriting History article on the official website.
git add .
will:Register all changes in the current content of your workspace (mind the
.
!) with respect to the current branch. This is also called “staging”.
Note
git add <path-to-file>
is used to stage changes in a specific file.git rm
is not the counterpart ofgit add
as it removes a file from the tracking and even from the workspace (unless you use--cached
).“unstageing” changes in a file can be done with
git restore --stage <path-to-file>
(see restore for details)
git add . -p
The -p
option allows to partially add files.
Running this command will allow to you to individual (de-)select
git commit -m 'my message'
will:Record all staged changes to the repository
Update the reference of the current branch to this last change
Note
Commit messages contribute drastically to the accessibility of a repository
If you struggle to create meaningful commit messages, consider reviewing what changes you pack into a single commit
git push
will:Update the corresponding branch on the remote repository with the changes from the current local branch
Note
You might encounter most hickups when doing a git push
.
However, this is only because the remote refuses any updates that cannot be fast-forwarded1If a new commit can be reached by following the history from another commit, then git can ‘fast-forward’ any reference to the new commit.
This operation highlights that your local branch is not up-to-date.
pull
In short, git pull
is a combination of two command: git fetch
followed by either git merge
or git rebase
.
It fetches the changes from the remote repository and integrates them into the current branch.
The integration happens either though a merge
or a rebase
and can be set by passing the options --rebase
or --no-rebase
to the git merge
command.
This is a very common operation when working with a remote repository and is often used to update your local repository with the changes from the remote repository.
git pull
procedure:Assume this is the state in which you run a git pull --no-rebase
:
C---D---E main on origin
/
A---B---C'---D' main
^
origin/main in your repository
Your local state of
origin/main
will be updatedorigin/main in your repository ∨ C---D----E main on origin / A---B---C'---D' main
origin/main
is integrated in yourmain
This happesn in two steps:
replay the commit from the remote branch (
C
,D
andE
) at the end of your local main, i.e. on top ofD'
register the result in a new commit
F
on top of your local branch, i.e. main in this case
origin/main in your repository ∨ C---D----E main on origin / \ A---B---C'---D'--F main