Notes of git commands from O'REILLY Media's Git and GitHub LiveLessons By Peter Bell.
-
Get current username :
git config --global user.name
-
Change / Set username :
git config --global user.name "New username"
-
Get current user emailId :
git config --global user.email
-
Change / Set user emailId :
git config --global user.email abc@xyz.com
-
cat ~/.gitconfig
==git config --global --list
-
Applies color :
git config --global color.ui true
-
Aliases for git commands: when you write
git alias.<aliasing-keyword> "<alias-command>"
it will run whole command
Click to see examples
git config --global alias.s "status -s"
git config --global alias.lg "log --oneline --all --graph --decorate"
-
Starts your project with (Best practice) :
git status
-
Initialize a git repo :
git init <repo-name>
orgit init
-
Adding files into staging area :
git add <file-name1, file-name2...>
-
Staging area is like shopping cart (Order will not be placed until you hit ORDER NOW button). Files will not be saved into history until you commit.
-
Commiting file :
git commit -m "<message>"
-
root-commit
= the very first commit, it appers only once in git repo -
SHA1 Hash
is used to uniquely identify each commit, therefore, each commit ID is suficiently long. -
Uploading repo to Github :
git remote add origin <repo-url>
-
Pushing local repo to Github :
git push -u origin <remote-branch-name>
-
Deleting file :
git rm <file-name>
– This auto stages but doesn't commit -
Rename file :
git mv <ocurrent-name> <new-name>
– This auto stages but doesn't commit -
If you rename a file (not via git) then git understands that you've deleted previous file and added a new file. While staging, it runs through the renamed file(s) and see if similarity index of previous file and renamed file is more than 0.5 then it recognises it as renamed and associate all commit history of previous file with renamed file otherwise if it is less than 0.5 then it deletes previous file and NEW commit history starts from renamed file.
Don't make big changes to your file contents in the same commit, as you are renaming it.
-
Ignore file : create a file
.gitingnore
What to write? eg :-*.log
– This means ignore all .log files in current directory and all of it's subdirectories. -
Exclude certain file(s) :
git config --global core.excludefile .gitignore
Don't exclude .gitingnore file, because, if you're ignoring some kind of files then you must not allow other collaborators to commit those files into your repo. For example,
.DS_Store
,.iml
-
Git ignore precedence : global
.gitignore
>> project.gitignore
>> subdirectories.gitignore
-
Include some ignored file, write
!<file-name>
after ignoring files
eg:– sequence matters
.gitignore:
| DON'T WRITE
1. *.log | 1. !index.log (It will first not ignore index.log
2. !index.log | 2. *.log file then it will ignore all .log files)
-
Add and commit files in same command :
git commit -am <message>
– This works on only modified (not untracked) files -
Show current branches :
git branch
-
Create a new branch each time you are adding a new feature (Best practice).
-
Create a new branch :
git branch <branch-name>
-
Switch to another branch :
git checkout <branch-name>
-
Create a new branch and switch to it :
git checkout -b <branch-name>
-
Merge a branch into another branch :
git merge <branch-name>
– you should be on the branch in which you want to merge another branch -
Delete a branch :
git branch -d <branch-name>
-
2 types of merging:
- Recursive merge happens when 2 diff branches have atleast 1 commit in each branch and you want to merge both branches.
- Fast-Forward merge happens when 2 branches share same commit history. If you merge them you'll lose track of commit history in new created branch.
-
No fast-forward merge (no-ff) :
git merge --no-ff <branch-name>
– This will do recursive merge and keep history of another created branch. -
See diff in UNSTAGED files :
git diff
-
See diff in STAGED files :
git diff --staged
-
See diff b/w working directory and the last commit :
git diff HEAD
-
Rebase branch :
git rebase <to-branch>
– you should be on the branch which you are rebasing -
See content of object file :
git cat-file -p <few-words-of-object-file-in-.git-folder>
-
Add alias to remote server :
git remote add origin <remote-server-link>
-
Pushing everything in current branch (commit history, files etc.) to remote server :
git push -u origin master
-
Pull changes on remote server repo and merges with local git repo :
git pull
-
Fetch changes on remote sever repo :
git fetch
-
Pull = Fetch + Merge
-
Change to remote server branch :
git checkout origin/master
-
Fetch + Rebase =
git pull --rebase
-
Copy in local :
git clone <repo-url>
-
Add version tag :
- Regular tag :
git tag v1.0.0
- Annotated tag :
git tag -a v1.0.0 -m "<message>"
- Signed tag : adds security
- Regular tag :
-
Push relase tags into Github :
git push --tags
-
Undo things :
git revert <few-words-of-the-commit-HASH-you-want-to-undo>
-
Change latest commit message :
git commit --amend
-
Throw away commits :
git reset --<type> HEAD~<number-of-commits-from-HEAD>
or,git reset --<type> <commit-hash>
<type>
:soft
: put those files in staging area, so that we can again commit ithard
: delete all those filesmixed
(default) : clear staging area but leave those files as untracked
-
Open time machine in git :
git reflog
-
Redo the commit :
git cherry-pick <commit-hash>
-
Undo things the way you want :
git rebase -i HEAD~<number-of-commits-from-HEAD>
-
Delete Remote Branch :
git push <remote_name> :<branch_name>
eg :–git push origin :serverfix