Skip to content

Latest commit

 

History

History
204 lines (158 loc) · 5.85 KB

git.md

File metadata and controls

204 lines (158 loc) · 5.85 KB

Git

Why you might use git??

  • Basically, one doesn't have to rename poject many times as "project(copy)(new)final-2.tex" etc etc etc. Git makes these much much easier to handle.
  • Moreover, many people can add soemthing into a project in an easy way.*
  • You can show/publish your code to the rest of the world.
  • Help pages/bio can also be published.

Technically, Git is a "free ... distributed version control system ... to handle ... small to very large projects with speed and efficiency."

Main commands of git

(One-time) Configure/Setup

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL ADDRESS"
git config --global push.default simple

For authentication, it is easiest to use ssh or secure shell See this

To initialize a git repository(=repo), you may do:

git init <directory>

To clone some existing repo (in say github), you can do something like:

git clone [email protected]:<username>/<repo>.git

Main workflow of git

  • To update your local repository
git pull
  • To add something/to stage changes for commiting:
git add <filename>
  • To commit changes:
git commit -m "your message"
  • To push these changes to the remote repository:
git push

Another two common functions

To check the current changes after adding a file etc, use:

git status

To see the exact differences in the file(s):

git diff

Terminologies

  • local repository -- repository in your local machine.
  • remote/upstream repo(sitory) -- If you store your project into some online repository (like github/gitlab/bitbucket) privately or publicly - this is where you store it.
  • branch -- Your git repo(sitory) can have morre than one branch - maybe one for final product, maybe one for development type 1, one for development type 2 etc. You can copy/merge one branche('s) file to another. So you can experiment in dev branch, then merge it to the master branch.
  • commit (as verb) -- 'The action of storing a new snapshot of the project’s state in the Git history, by creating a new commit representing the current state of the index and advancing HEAD to point at the new commit.'
  • commit (as noun) -- A single point in the Git history; the entire history of a project is represented as a set of interrelated commits. Synonymous to "revision" or "version."
  • Tracked and untracked files - files either in the index cache or not yet added to it
  • Cache - a space intended to temporarily store uncommitted changes
  • Stash - another cache, that acts as a stack, where changes can be stored without committing them

Specific commands

Git add or deleting

To add two files and/or whole directory at once,

git add <filename_1> <filename_2> <directory_1> 

To add all the files under the current directories,

git add .

To remove a file/directory, use similar coommands like

git rm <filename>

Git diff

To get the diff for the Staged file (after git add . or equaivalent)

git diff --staged

diff among branches

git diff branch_1...branch_2

Get differences of two specific files:

git diff branch_1..branch_2 -- path/to/myfile
# or
git diff branch_1 branch_2 -- path/to/myfile

Only to know which files differ

git diff --name-only branch_1...branch_2

Compare checked out branch to branch_2 source:

git diff ..branch_2

Checkout one file from another branch

syntax:

git checkout branch_name -- file_name

e.g.:

git checkout master               # first get back to master
git checkout dev -- app.js	  # then copy the version of app.js from branch "dev"

Or Update August 2019, Git 2.23 With the new git switch and git restore commands, that would be:

git switch master
git restore --source dev -- app.js

git log

git log

show recent commits, most recent on top. Useful options: --color with color --graph with an ASCII-art commit graph on the left --decorate with branch and tag names on appropriate commits --stat with stats (files changed, insertions, and deletions) -p with full diffs --author=foo only by a certain author --after="MMM DD YYYY" ex. ("Jun 20 2008") only commits after a certain date --before="MMM DD YYYY" only commits that occur before a certain date --merge only the commits involved in the current merge conflicts. source

Remotes

git tag

You can give tags to some commits which you think are important, like:

git tag <tag-name>

Then push it to remote repository by:

git push origin <tag-name>

If you want to push all tags, you may try git push --tags

To remove a tag:

git tag -d <tag-name>

and to push that to remote

git push --delete origin <tag-name>

Catfile

Extract and dump a file of a previous version.

git cat-file -p commitid:./check.tex > ./check2.tex

commitid can be anything:

  • symbolic ref (branch, tag names; remote too)
  • a commit hash
  • a revision spec like HEAD~3, branch1@{4} etc.