Version control for local files using GIT
This guide shows how to have local files under version control using git. It does not cover how to clone remote repositories (there are plenty of guides already available for that).
Setting up a git repo
$sudo apt-get install git
$git config --global user.name "Vaivaswatha N"
$git config --global user.email "email@example.com"
$git config --global core.editor emacs
$git add .
Basic version control using git
Mark modified files for commit: This command will add files to the staging area
, which means its marked for commit, and will be committed the next time you run “git commit”. If you make any changes to files after “adding” them, those changes will not make it to the commit, you will need to again “add” the file.
$git add <filename(s)>
$git checkout <filename>
$git checkout .
$git revert SHA1
$git diff <filename>
This will display differences between the repo copy and the modified copy of the file.
View changes from a commit: While “$git diff SHA1 SHA2” in most cases shows the diff between two commits, with merges in between, it may be simpler to use below command to see changes from a particular commit.
$git show SHA1
$git checkout -b <NewBranchName> SHA1
$git checkout <BranchName>
$git branch -D <BranchName>
$git merge <BranchName>
$git rebase master
Typical workflow is to branch out (say branch1) from master, do your work in “branch1” and commit it. Then switch to master and merge “branch1” to it. Changes from different branches come into master this way (via merges). Your branch can keep updated with the master by rebasing against it. Note that rebasing will cause all commits since divergence from base to be re-committed. This will create a mess if your local branch is tracking a remote branch and you push to the remote branch. Use `git merge` to merge from the base, instead.
Cherry pick a change from another branch, instead of merging all of it. i.e. Instead of all changes “till” a specific commit (or the HEAD) (which is what “git merge” would do), just pick *only* a specific commit.
$git cherry-pick SHA1
$git stash pop
$git remote add origin path://to/git/repo
$git remote update
$git checkout -b branchname origin/branchname
$git push -u origin branchname
$git pull repo branchname
$git commit --amend
$git config --global alias.lg "log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all"