$ git init
will initialize git repository in current directory
$ git status
to see current state of project
$ git add file.txt
to start tracking changes made to file.txt...added to staging area
$ git commit -m "just added the file to repository"
moving file from staging area to repository
$ git add -all
(or)
$ git add .
adds all new or modified files
$ git add *.txt
adding files of same type in current directory
$ git add docs/*.txt
adds all txt files in docs directory
$ git add docs/
adds all files in docs directory
$ git add "*.txt"
adds all txt files in the whole project
$ git add -u
stage tracked files, including deleting the previously tracked files
$ git add -u .
stages tracked files in current path (including sub folders)
$ git add -u :/
stages whole working tree
$ git log
to browse what files are changed
$ git log -1
to check last commit
$ git log --graph --decorate --oneline
$ git remote -v
to know about remote repos
$ git remote add origin https://github.com/vinkrish/repo-name.git
to push local repo to GitHub server...takes remote name and repository URL
$ git remote rm upstream (or) origin
to remove upstream/origin
Fork the repo first
$ git remote rename origin upstream
$ git remote add origin URL_of_forked_repo
while working on forked repo change origin to point to forked repo
$ git remote add <name> <address>
to add new remotes
$ git remote rm <name>
to remove remote
$ git remote show origin
$ git remote prune origin
removes all stale branches
$ git push -u <name> <branch>
to push to remotes, is usually master
$ git push -u origin master
name of our remote is origin and default local branch name is master.
-u tells Git to remember the parameters and next time don't have to specify origin and branch
$ git pull origin master
pull down any new changes
To pull new branch from upstream
$ git fetch upstream
eg: git fetch origin branch_name
$ git pull upstream/branch_name
eg: git pull upstream master
$ git diff
shows unstaged differences since last commit
$ git diff HEAD
diff of our most recent commit, HEAD refers to last commit.
$ git diff --staged
$ git diff --cached
changes that are staged, to show diff after adding files.
$ git diff HEAD HEAD~2
$ git diff branch_name master
changes in branch compared to master
$ git revert commit_hash
creates a new commit that undoes the changes from a previous commit (adds new history to the project)
Reset moves the current branch and optionally copies data from repository to other areas
$ git reset HEAD directory/file.txt
$ git reset directory/file.txt
unstage files..removing
$ git reset --soft HEAD^
reset into staging, move to commit before HEAD
$ git reset --hard HEAD^
$ git reset --hard <previous commit hash>
undo last commit and all changes
$ git push -f origin main(or master)
To make changes to remote
$ git reset --hard commit_ref => copies data into Working Area & Index
$ git reset --mixed commit_name => copies data into Index, default behavior
$ git reset --soft commit => doesn't touch any
$ git commit -a -m "add changes from all tracked files"
If you want to skip the staging area and not add new files
$ git add forgottenFile.txt
$ git commit --amend -m "modify last commit with this new commit message"
add to the last commit
$ git checkout -- file.txt
changes back to how they were at the last commit for file.txt
Branching Out: when you are working on a bug or feature, u can create copy(aka branch) of code and can make separate commits to.
when u r done u can merge this branch back into their main master branch.
$ git branch clean_up
branch is called clean_up
$ git checkout -b branch_name origin/branch_name
copy the remote branch
$ git branch
lists local branches: master and new clean_up branch
$ git branch --all
lists are remote branches as well
$ git checkout clean_up
will start using new branch
$ git rm '*.txt'
in clean_up branch we can remove file or use wildcard
$ git commit -m "Remove all the files"
after removing we need to commit changes
$ git branch -d branch_name
delete local branch
$ git branch -D branch_name
delete local branch forcefully (irrespective of its merged status)
$ git push -d origin <branch-name>
$ git push origin --delete <branch_name>
delete remote branch
$ git checkout master
after finishing with branch, switch back to master..copy or merge from clean_up branch to master branch
$ git checkout master
$ git merge clean_up
$ git branch -d clean_up
delete clean_up branch after merging changes to master
$ git checkout commit
instead of checkout branch, you can directly checkout commit which will result your HEAD in detached state
$ git push
to push files from now on
$ git clone url
to fetch files from github
$ git checkout -b new_branch
creates and checks out branch
$ git pull origin new_branch
$ git checkout master
$ git merge new_branch
:wq
hit Enter to write(save) & quit
When there is conflict fix it manually, then add and commit without commit message.
$ git add conflicted_file
$ git commit
$ git merge new_branch
After branch is merged to master
$ git checkout new_branch
$ git merge master
this is fast-forward
$ git commit --amend
to update last commit without creating new commit, git does that for you
mv old_name new_name
$ git add new_name
$ git add old_name
This removes old_name file from index and it does that by renaming coz content of both files are same.
$ git mv new_name old_name
built in way of renaming
Please move or remove them before you can merge:
$ git clean -d -f
Unrelated history issues
$ git pull origin master --allow-unrelated-histories
$ git push origin master
cherry-pick: Apply the changes introduced by some existing commits
$ git cherry-pick commit_hash
If new commit message is needed:
$ git cherry-pick --edit commit_hash
Tagging:
$ git tag
v0.0.1
v0.0.2
$ git checkout v0.0.1
$ git tag -a v0.0.3 -m "version 0.0.3"
$ git push --tags
$ git fetch
$ git rebase
$ git add file
$ git rebase --continue
$ git rebase --interactive
$ git rebase -i
$ git log --oneline --graph
$ git log --until=1.minute.ago
$ git log --since=1.day.ago
$ git log --since=1.hour.ago
$ git log --since=1.month.ago --until=2.weeks.ago
$ git log --since=2000-01-01 --until=2012-12-21
$ git reflog HEAD
$ git reflog refs/heads/master
$ git show commit_log
$ git show HEAD
$ git show HEAD^
$ git show HEAD^^
$ git show HEAD~2
$ git show HEAD~2^2 => 2nd parent of 2nd commit before HEAD
$ git blame file => which commit changed what
$ git blame index.html --date short
.git/info/exclude
experiments/
will exclude this folder from git
.gitignore
logs/*.log
To remove file from repo
$ git rm file.txt
-f force removal
--cached only removes from Index but keeps it in Working Area
$ git rm --cached file.txt
untracking files but not deleted from file system
git ls-files --deleted -z | xargs -0 git rm
git config -l
$ git config credential.helper store
$ git push https://github.com/repo.git
Username for 'https://github.com':
Password for 'https://USERNAME@github.com':
git config --global user.name "Vinay Krishna"
git config --global user.email "vinaykrishna1989@gmail.com"
git config --global color.ui true
git config --list
git config --list --global
git config --list --system
git config --list --local
git config --edit --global
git config --edit --system
git config --edit --local
heroku create
git push heroku master
git reset should be used before push
$ git help log
$ git log --patch
$ git log --grep context_text --oneline
$ git help grep
$ git log -3 --oneline
Commands that move branches:
commit, merge, rebase, pull, reset
checkout changes repo coz it moves head pointer & copies data into Working Area and the Index
For large file:
brew install git-lfs
git lfs install
git lfs track "*.csv"
git add .gitattributes
git add file.csv
git commit -m "Add data file"
git push origin master