GIT COMMANDS
Documentation
ZIYAD MANSY
Mobile Software Engineer
Basic git Commands Explanation
git --version Checks current Git installed version
Initializes an empty git repository locally in the project to start
git init
working with git for this project
git add .
Adds newly created/updated files as Staged Files to staging area
git add <FILE NAME>
git commit -m “<MSG HERE>” Commits all staged files with a message
git branch Lists all branches
Gets the status of the current branch for all committed/added
git status
files
Gets the history of all commits for the current branch and some
git log
information about each commit (author, date, hash)
git branch <BRANCH NAME> Creates a new branch
git checkout <BRANCH NAME> Switches to the specified branch name
git checkout -b <BRANCH NAME> Creates then switches to the new branch in one step
Switches to the specified branch name.
git switch <BRANCH NAME> Same as (git checkout) but works for branches only without
commits
Creates then switches to the new branch in one step.
git switch -c <BRANCH NAME> Same as (git checkout -b) but works for branches only without
commits
git ls-files Lists all files/folders in the staging area
Tracked files are files that either added or committed to the
Tracked Files
current repository branch
Staged files are files that were only added (git add) to the
Staged Files
current repository branch
Committed files are files that were add and committed (git
Committed Files
commit) to the current repository branch
Untracked/Unstaged files are files that neither added nor
Untracked/Unstaged Files
committed to the current repository branch
© 2021 Ziyad Mansy
git rm <FILE NAME> Deletes the specified tracked file from the working directory
git checkout .
Undo all unstaged changes to the current branch and goes back
or
to the HEAD of all commits for the current working branch
git checkout <FILE NAMEs>
git restore . Same as (git checkout), Undo all unstaged changes to the current
or branch and goes back to the HEAD of all commits for the
git restore <FILE NAMEs> current working branch
Lists all untracked files/folders that would be cleaned(deleted)
git clean -dn
as a warning message
git clean -df Forces the deletion of all untracked files
Undo staged changes for the specified file and revert it back to
an untracked file at the HEAD of the branch without removing
git reset <FILE NAME> the modifications. To the moment before the step of (git add) to
be able to use (git checkout or git restore) to remove the
modifications
Same as (git reset), Undo staged changes for the specified file
and revert it back to an untracked file at the HEAD of the branch
git restore --staged <FILE NAME> without removing the modifications. To the moment before the
step of (git add) to be able to use (git checkout or git restore) to
remove the modifications
Deletes number of commits equal to the specified number
starting from the old HEAD to the newly reached one.
git reset --soft HEAD~<NUMBER>
It deletes only the commit and reverts it back to the staged area
and still exists in the working directory
Deletes number of commits equal to the specified number
starting from the old HEAD to the newly reached one.
git reset HEAD~<NUMBER>
It deletes both the commit and the staged area and still exists in
the working directory
Deletes number of commits equal to the specified number
starting from the old HEAD to the newly reached one.
git reset --hard HEAD~<NUMBER>
It deletes the commit and the staged area and even from the
working directory
Only allow to delete the specified branch if we merged this
git branch -d <BRANCH NAME>
branch already.
Forces the deletion of the specified branch no matter we merged
git branch -D <BRANCH NAME>
the branch or not.
HEAD theory It is the last commit for each branch
© 2021 Ziyad Mansy
DETACHED HEAD theory It is the commit that you checked out to it to edit/view it
It checks out a specific commit and deal with it as a separated
git checkout <COMMIT HASH> isolated branch (Called: DETACHED HEAD) to view/edit it
then go back(checkout) to your desired branch normally
When we checkout to a specific commit with:
(git checkout <COMMIT HASH>)
It only creates a temporary branch of this commit only.
To edit this commit specifically we follow the following
procedure to be able to merge this edit to the HEAD of the
current branch:
Detached HEAD theory - Make the changes to the commit.
- Commit the changes.
- Create a new branch (It will automatically take a copy of
all commits of the detached head temporary branch).
- Switch to the current desired branch.
- Merge the newly created branch with the current branch.
- (Optional) Delete the newly created branch.
© 2021 Ziyad Mansy
Advanced git Commands Explanation
Saves uncommitted / unstaged changes to memory for the current
git stash
branch and goes back to the HEAD of all commits for the current
git stash push -m “MSG HERE“
working branch
Loads the stash according to the specified number if exists
git stash apply
otherwise it loads the latest stash on the stack then apply it on the
git stash apply <NUMBER>
current working branch
Lists all stashes with their indices as a stack.
git stash list The stash at the top with smallest index is the latest stash and vice
versa
Removes the stash according to the specified number from the
stack of stashes and apply it on the current working branch.
git stash pop <NUMBER>
Same as (git stash apply <NUMBER>) but deletes the stash from
the stack afterwards.
git stash drop <NUMBER> Deletes the specified number stash from stash list.
git stash clear Deletes all stashes from stash list.
Lists all the history of all actions (commits, deletions, resets, etc.).
It assigns a hash ID for each action to enable us to restore this
git reflog
action whenever we want even after deleting something.
Hint: it has 30-days’ time limit for storing actions
To restore a lost commit:
- Use (git reflog) to locate the lost commit
- Copy the Hash ID
- Use (git reset --hard <HASH ID>) to reset the Head to the
lost commit
To restore a lost branch:
Reflog theory - Use (git reflog) to locate the latest lost commit (that has the
latest status of that branch) made in the lost branch
- Copy the Hash ID
- Use (git checkout <HASH ID>) to be on the Detached Head
of this Commit in a temporary branch
- Create a new branch as a replacement of the lost branch (it
will copy the Detached Head commits)
Merges the specified branch with the current working branch using
the suitable strategy with updating the Head to the last added
git merge <BRANCH NAME> commit.
(All specified branch new commits will be added to the current
working branch as committed files)
© 2021 Ziyad Mansy
Merges the specified branch with the current working branch using
the suitable strategy without updating the Head.
git merge --squash <BRANCH
(All specified branch new commits will be added to the current
NAME>
working branch in the staging area waiting to be committed as a
single commit)
git merge --no-ff <BRANCH NAME> Forces the merge strategy to be Non-Fast-Forward strategy
Merge types:
- Fast-Forward
Merging happens when there are no additional commits
made in the current working branch after the specified
branch was created.
It simply copies the head of the specified branch to the
current working branch and doesn’t create a new commit.
To delete the merge:
Use (git reset --hard HEAD~<NUMBER>) to delete all the
newly added commits after the merging.
- Non-Fast-Forward
o Recursive
Merge theory Merging happens when there are additional commits
made in both the current working branch and the
specified branch after the specified branch was
created.
It merges the specified branch with the current
working branch with additional (merge) commit is
created in the current working branch.
To delete the merge:
Use (git reset --hard HEAD~1) and only delete the
automatically added merge commit and it will delete
its merge commits accordingly automatically.
o Octopus
o Ours
o Subtree
Aborts the merge and returns to the state before we apply the
git merge --abort merging.
Used to abort merging in case of merge conflicts.
Lists the commits that will be merged from the current branch and
the specified branch and some information about each commit
git log --merge
(author, date, hash)
Used after merging conflict.
Lists the differences between the two branches that causes the
merging conflict.
git diff
Gives insights of what the problems are and lists them in a readable
format.
© 2021 Ziyad Mansy
Copies the specified commit using is Hash ID from any branch to
git cherry-pick <COMMIT HASH>
the Head of the current working branch with a new Hash ID.
git tag Lists all the tags for all commits in all branches
Tags the specified commit with a lightweight(temporary) specified
git tag <TAG> <COMMIT HASH>
tag.
git show <TAG MSG > Shows the log of the specified tag commit.
It checks out a specific commit using its tag and deal with it as a
separated isolated branch (Called: DETACHED HEAD) to
git checkout <TAG> view/edit it then go back(checkout) to your desired branch
normally.
Same as (git checkout <COMMIT HASH>)
git tag -d <TAG> Deletes the specified tag.
Tags the Head Commit in the current working branch with an
git tag -a <TAG> -m <TAG MSG> annotated tag including more details (tag, tag msg, tagger, date,
etc.…).
© 2021 Ziyad Mansy
Remote git Commands Explanation
git remote add origin <REPO
Links the local repository with the specified remote repository.
URL>
Uploads the current working local branch to the remote cloud repository
git push origin <REMOTE
to the specified remote cloud branch. If the branch doesn’t exist, it will
BRANCH>
create the remote branch and push to it automatically.
git pull origin <REMOTE Downloads the specified remote branch updates to the current working
BRANCH> branch.
Lists the following branches:
- Local branches
git branch -a
- Remote tracking branches
- Local tracking branches
Lists the following branches:
git branch -r
- Remote tracking branches
Lists the following branches:
git ls-remote
- Remote cloud branches (ex: Branches on GitHub)
Same as (git branch) but provides more details for each local branch to
git branch -vv differentiate between local and local tracking branches and their
connected remotes.
git fetch Updates the remote tracking branches with the latest remote branches.
Creates a local tracking branch with the specified name connected to the
specified remote tracking branch.
git branch --track <BRANCH
Hint: It should be named the same as the specified remote tracking
NAME>
branch.
remotes/origin/<REMOTE
Used to use (git push, git pull) without specifying a specific branch origin
BRANCH NAME>
each time. It automatically pushes/pulls form its connected remote
branch.
Local Copy of Remote Branch which can get the latest update using:
(git fetch).
Remote tracking branches They are branches that act as a middle layer between the local branches
theory and the remote cloud branches.
They are still local on the machine and are required to push/pull to it and
it will automatically connect with the remote cloud branch.
Local cache of remote tracking branch that can be edited.
Local tracking branches theory
Connected to remote tracking branch that gets the latest changes.
© 2021 Ziyad Mansy
Branch Types
Clones a remote cloud repository into a local repository.
git clone <REPO URL>
Hint: It is better to create local tracking branch for each remote cloud branch.
When using the upstream, ex: git push -u origin feature.
Upstream theory If that remote branch doesn’t exist, it will create it then it will convert the local
branch pushing to it into a local tracking branch.
git branch --delete --remote
Delete a remote tracking branch.
origin/<REMOTE BRANCH>
git push origin --delete Deletes a remote cloud branch.
<BRANCH NAME> The corresponding remote tracking branch will be deleted automatically.
git push --force origin master Forces the push of local branch to remote branch.
© 2021 Ziyad Mansy