Important Git Facts

From genjusz.wordpress.com Git — setting up a remote repository and doing an initial “push”

Keep My GitHub Fork Up-to-date

Git home

When you clone a project/repository you are copying all its files and metadata.

Staged means that you have marked a modified file in its current version to go into the repository the next time you commit. Or, the term staged could refer to a new file being marked for inclusion in the next commit.

This file will be committed in the state it was in when you staged it.

A working directory is a single checkout of one version/branch of a project/repository.

Within the working directory you’ll find the project files (for the checked-out branch) and the git metadata of the project (in .git hidden sub-directory.)

If (later-on) you switch branches you Do Not have to look somewhere else in file system to find project files for the branch you switched to. They magically appear in the same place where the previous branch files were.

Index is a term which refers to the staging area.

File state:

  • modified
  • removed
  • renamed
  • tracked/(previously committed)
  • staged

$ git config

to alter git configuration files

You can configure at these “levels”:

  • --system
  • --global
  • current repository

You can set a user name and email which is different from the global one — on a per project basis.

You can check what Git thinks a specific configuration key’s value is by typing
git config {key}

Help!

$ git help <verb>
$ git <verb> --help
$ man git-<verb>

url = https://github.com/Sameh-R-Labib/CodeIgniter-3-Source-Explained.git

You will find a “text segment” like this in .git/config within your local repository’s folder. That’s the only place where you can find something that can be construed as a git repository name.

Here, CodeIgniter-3-Source-Explained will be the repository name — if there is such a thing as a repository name.

If I was creating that remote repository (w/o GitHub UI) using SSH from my computer at home I would have done:

mkdir CodeIgniter-3-Source-Explained.git
cd CodeIgniter-3-Source-Explained.git
git init --bare

This creates a repository without a .git directory or a working directory.

My workflow: — 1. create repo on GitHub … 2. clone it to my computer using git clone or GitHub.app

$ git init

You issue the git init command at a directory you want to turn into a repo. Issuing this command creates a new subdirectory named .git which contains all of your necessary repository files. You won’t be tracking anything yet — since you haven’t made any commits.

.git

is a hidden sub-directory of the working directory of a project

The Git repository is stored in this directory.

$ git clone https://github.com/username/Straw-Hey.git
# Clones your fork of the repo into the current directory in terminal

See GitHub instructions.

$ git clone git://gxsam11.net/samehlabib/customfw.git

is an example for cloning a remote repository

creates a directory named customfw, initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version.

I get the feeling this will be created in the current directory.

You can have this command give the working directory a different name by specifying it as a parameter (at the end.)

Git can use a variety of transfer protocols. The previous example uses the git:// protocol, but you may also see http(s):// or user@server:/path.git, which uses the SSH transfer protocol.

When you first clone a repository, all of your files will be tracked and unmodified because you just checked them out and haven’t edited anything.

$ git status

to determine which files are in which state … also indicates which branch you are on

After clone the status will be no (tracked && modified) files.

Several branches can exist within a single repository. Every branch has a name — Each repository has at least a master (that’s its name) branch.

git status command will tell you which files are untracked.

Untracked basically means that Git sees a file you didn’t have in the previous snapshot (commit); Git won’t start including it in your commit snapshots until you explicitly tell it to do so.

git add

to begin tracking a new file

If you commit at this point, the version of the file at the time you ran git add is what will be in the historical snapshot.

The git add command takes a path name for either a file or a directory; if it’s a directory, the command adds all the files in that directory recursively.

After a previously tracked file is modified it has to be staged in order to get committed in its new (modified) form.

The git add command can be used to mark merge-conflicted files as resolved.

git mv

mark files to be moved (renamed) — within the historical snapshots for the branch

git rm [file]

mark file to be removed from index (staging area); And, the file will be deleted from your working directory.

If you simply deleted the file yourself you still need to use this command before you commit. Doing a git status will show this.

git reset HEAD [file]

to unstage

git status

Use this before you commit to see which files need to be staged.

git diff [file]

or just

git diff — compares what is in your working directory with what is in your staging area

git diff --cached — command compares your staged changes to your last commit

Generally speaking — Use diff before you commit so you can see exactly what you’ve modified in a file — or all files which are in modified status.

shows you the lines added and the lines removed (the patch)

git commit

The simplest way to commit. But, will open an editor for you to enter the commit comment.

git commit -a

automatically adds modified files; then takes snapshot — Note: it won’t add the files which are new.

git commit -a -m [message]

specifies a message to associate with this commit

You will be committing to the current branch.

git log

shows a log of all commits for the branch

Historical snapshot == A commit stored in git

You may have a class of files which you are sure you don’t want Git track. If that’s the situation then create a file which lists all the patterns for the file names of files in that class. Name that file .gitignore. See Git website for more on this.

I believe .gitignore is similar to configuration files in that there are several places in the file system you can place them — but the most local setting for a particular key will apply.

$ cat [file]

useful UNIX command for displaying a file’s content

$ cat ~/.gitconfig

example

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in CI Source Code Explained, Configure My Systems, Other, UNIX Command Line. Bookmark the permalink.

One Response to Important Git Facts

  1. Pingback: My New Collaborative Blogging Workflow | Sam's PHP

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s