git fetch
: get remote commits to local repogit pull <repo> <branch-name>
e.g.git pull origin master
: get the commits from remote branch to current branchgit branch -a
: list all branches including remote branchesgit checkout <branch-name>
: switch to the branch
git branch -m <new-name>
: rename current local branch. Check this for more information.git checkout -b <new-branch>
: create a new branch based on the current branchgit checkout -b <new-branch> <existing-branch>
: create a new branch given an existing branch
git status
: list of files tracked and not trackedgit add .
orgit add <file-names>
: add files to staging areagit add "*.py"
: add all python files to staging area
git push <repo> <branch-name>
e.g.git push origin master
: push the local changes to remote branchgit push -f origin master
: force local updates to be pushed to remote- discard the local changes from working area :
git checkout -- <file-name
git fetch
git checkout origin/master -- path/to/file
: checkout a particular file. See this for more information.git remote show url
: show the remote repo url. Check this link for more details.git branch -m new-name
: rename current branch. Check this for more info.
git stash list
: list all git stashesgit stash apply <number>
: apply particular git stash or the recent one (0) if no number is providedgit stash drop <number>
: drop the particular git stash or the recent one (0) if no number is providedgit stash clear
: clear all git stash form the listgit stash push -m welcome_cart app/views/cart/welcome.thtml
: stash particular files. Check this for more information.git stash save "my_stash"
: save stash with a message
There are several ways to undo a mistake, each with a different outcome. The best one is to use git revert
.
Check this link and this tutorial.
git revert HEAD
- revert the immediate last commit by making exact opposite commit.git revert HEAD~2
- go back two commits.git revert <sha1-1>..<sha1-3>
- go back to the previous commit ranges. View git branches in terminal in nice graph form -git log --graph --decorate --oneline
Check if git pull
is needed: git remote -v update
Add an empty directory to the repo (useful when want to store stuff that should not be sent to remote)
- create directory (e.g.
.logs
) - inside
.logs
create.gitignore
and add following:# Ignore everything in this directory * # Except this file !.gitignore
Another tutorial link by GitHub to undo stuff using git
.
git fetch --all
git reset --hard origin/master
git pull origin master
git config --global color.ui true
This is particularly useful when we want to copy files from a remote repo. Usually this happens when we fork from a repo, or build upon the code from the repo and there was an update in the original repo and we want to (lazily) include all the changes from the update to our code. See this link for more information.
git remote add alt <alt-machine:/path/to/repo>
git remote update
git pull alt master
If you do a git remote -v
then you will see the added remote also. If you want to remove tracking for the new repo then run git remote remove <remote>
e.g., git remote remove alt
. After this the remote branch should have been removed. Remember to commit the changes from the remote before removing the remote repo tracking.
git diff --stat --color master..branchname
Check this link for the above command. If want to see the individual changes in a particular file, modify the above command as:
git diff mybranch..master -- myfile.cs
Check the name of the file in which a merge conflict is present
git diff --name-only --diff-filter=U
Git showed a message to this link for large files remote storage.
Syncing a fork with another remote: link