Basic Git Commands
Init
Initialize local git repository.
Create new local git repo
git init
Clone
Clone or download remote repository.
Cloning remote repository
git clone https://github.com/<repo-owner>/<repository>.git [cloned-repo-name]
[cloned-repo-name]
is optional to name the repository directory other than the original name from the remote.
Status, Add, Commit and Log
Check git local repo status
git status
Stage new changes for commit
git add <filename>
git add .
β Adds all from the current directory (including subdirectories)
Commit staged changes
git commit
β Default CLI editor will open commit messagegit commit -m "Add helpful commit message here"
Display commit history
git log
50/72 rule suggest to compose not more than 50 characters (72 MAX) commit message for cleaner commit history.
Ref:
Push
Pushing commits to remote branch
git push
β push all commits to remote branchgit push --force
β forcefully push all commits to remote branch. This will rewrite commit history of remote branch if local branch diverged.
Pull
Updates local branch of new changes from remote branch
Pull new changes from remote branch
git pull
git pull --all
β update all local branches
Fetch
Fetch new changes from remote branch without applying those changes yet.
Fetch new changes from remote branch
git fetch
git fetch --all
β fetch all local branches
Display new changes from remote branch after fetching
git log origin/<branch-name> ^<branch-name>
# or
git log --oneline --decorate origin/<branch-name>
# or for files which will be modified with `git pull`
git fetch && git diff HEAD @{u} --name-only
# or all the difference between the current version and the incoming
git fetch && git diff @{u} --name-only
Diff local branch against new changes from remote branch after fetching
git diff <branch-name> origin/<branch-name>
Checking out latest commits from remote branch after fetching
git fetch
git checkout <branch-name> origin/<branch-name>
Ref:
Branch
Branch other than the master branch to work on the code base without affecting the remote main branch (master). Use merge
or rebase
to apply changes from one branch to another.
Display all local branches
git branch
Display all local and remote branches
git branch -a
Renaming current branch
new-branch-name>
git branch -m <new-branch-name>
git push origin -u < git push origin --delete <old-branch-name>
Switching local repo to different branch
git checkout <branch-name>
Creating new branch
git branch <new-branch>
git checkout -b <new-branch>
β Creates new branch and checkout.
Pushing newly created local branch into remote repository
git push -u origin <branch-name>
Delete existing branch (Only locally)
git branch -d <branch-name>
β Wont work if theres unmerged changesgit branch -D <branch-name>
β Force delete while theres still unmerged changes
Delete existing branch from remote after local deletion
git push origin --delete <remote-branch-name>
Using checkout
command to dispose changes (replacing local changes with server copy).
WARNING: This cannot be undone.
git checkout <filename>
git checkout .
β Will dispose all changes in current directory
Ref:
- πΊ Git Branch, Checkout Commands Tutorial
- π How to Delete a Git Branch Both Locally and Remotely
- π How can I tell a local branch to track a remote branch?
- π Rename a Local and Remotee Git Branch
Checkout
Checking out to previous commits
git checkout <commit-sha>
β Checkout to previous commit.<commit-sha>
is the unique commit SHA digits of the commit. Can be found ingit log
git checkout <branch-name>
β Going back to latest commit in branch
NOTE:
git reflog
can also be a checkout reference point.
Checkout back to last checkout point
git checkout -
Ref:
Merge
Merge changes of one branch to another branch.
Merging branch to master branch
git checkout master
git merge <branch-name>
Merging master branch to another branch
git checkout <branch-name>
git merge master
Ref:
Rebase
Rebase (re-anchor) current branch commit history against another branch to latest changes.
Rebase a branch against master branch
git checkout <branch-name>
git rebase master
Rebase master branch against another branch
git checkout master
git rebase <branch-name>
Undoing rebase
git reflog
β Logs all reference point to reset intogit reset --hard HEAD@{<ref-log-number>}
β Reset back to specific references- Sample:
git reset --hard HEAD@{5}
will reset 5 reference point back
Ref:
Stash
Changes that are not ready to be committed but wants to revert back to previous commits without loosing those changes can be stashed. It will be saved in a temporary space and can be accessed and applied at anytime and any branch or commit history.
Stashing current changes
git stash
git stash save "<message>"
β With stash message
NOTE: You can add the
-p
flag to interactively select hunks from diff or changes.
NOTE: Stashing changes will be kept until βunstashedβ applied.
git status
orgit diff
will not show the changes that has been stashed.
Listing all stashed changes
git stash list
Show all stashed changes
git stash show
β Show changes from the first stash on thegit stash list
git stash show stash@{<stash-number>}
β Show changes of specific stash on thegit stash list
Applying stashed changes
git stash apply
β Applies the first stash on thegit stash list
git stash apply stash@{<stash-number>}
β Apply specific stash fromgit stash list
NOTE: Applying stash does not delete the stash.
Drops (delete) a stash
git stash drop
β Drop the first stash on thegit stash list
git stash drop stash@{<stash-number>}
β Drop specific stash fromgit stash list
git stash clear
β Drops all stash
Apply and drop (delete) stash
git stash pop
β Apply and drop the first stash on thegit stash list
git stash pop stash@{<stash-number>}
β Apply and drop specific stash fromgit stash list
Ref:
Clean
Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.
[path]
is optional
git clean [path]
β Clean working tree directory (non-recursive)git clean -d [path]
β Clean working tree recursivelygit clean -i [path]
β Clean working tree interactivelygit clean -f [path]
β Clean working tree forcefullygit clean -n [path]
β Dry-run clean working tree
Ref:
Forking Repos
Make a copy of someone elseβs repository and make it as your own. You can make pull requests to propose updates from your fork to the original repository, once approved by the original owner, will merge all commits in forked repo from when the pull request was made.
Updating your Fork
# add the original repository as remote repository called "upstream"
git remote add upstream https://github.com/<original-owner>/<repository>.git
# fetch all changes from the upstream repository
git fetch upstream
# switch to the master branch of your fork
git checkout master
# merge changes from the upstream repository into your fork
git merge upstream/master
Ref:
Resources
- π Git 50/72 Rule
- π Where to find changes due to
git fetch
- πΊ Git Branch, Checkout Commands Tutorial
- π How to Delete a Git Branch Both Locally and Remotely
- π How can I tell a local branch to track a remote branch?
- π Rename a Local and Remotee Git Branch
- π How to get back to the latest commit after checking out a previous commit?
- πΊ Merging Branches Tutorial
- πΊ A Better Git Workflow with Rebase
- πΊ Git MERGE vs REBASE
- π Undoing a git rebase
- πΊ Git Tutorial: Using the Stash Command
- π Git stash official docs
- π Git clean official docs
- π Githubβs Fork & Pull Workflow for Git Beginners