Part3GitBasics - xorkevin/GitIntroduction GitHub Wiki

#part 3: git basics

The purpose of git is to allow for people to collectively program. In order to achieve this, git uses a remote and many local distributions of a repository that are all interconnected which was exposed in part 1.

By now, you should have already cloned the repository from part 1.

This part will go into more depth about the fundamental aspects of git.

  • one note before we start: This tutorial merely covers the basics. If there are any other questions about git, the command line tool git help is your friend.

##Committing

Committing is the process by which changes that have been made to the files are recognized by git and saved as a commit into the Git Log.

###prerequisite steps

  1. navigate to the TestRepo directory.

  2. open up the README.md using either Notepad or (MarkdownPad which was used to write these tutorials).

  3. add your name underneath the current list of names like so

  • first_name last_name
    
    and save the file.
    
    
  1. now open up git bash in the TestRepo directory and prepare to learn the following git commands:

###git status

git status is a simple command to view what files have been changed - added, deleted, or modified. It also shows what remote branch it is tracking.

here is some sample output:

user@computername /C/path/to/your/TestRepo (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
	(use "git add <file>..." to update what will be committed)
	(use "git checkout -- <file>..." to discard changes in working directory)

		modified: 	README.md

no changes added to commit (use "git add" and/or "git commit -a")

As you can see, git states that it is tracking origin (aka the repository hosted on Github) for changes on the branch master. It then states that, in this case, your local repository is up-to-date with the repository on Github.

Next, it shows what files you have changed and even helpfully suggests a few commands (git add, git commit) which will be covered later.

git status is a helpful command in determining what files you have changed since the last commit, and determine if there were any changes made to the Github repository since you last obtained the files from the server.

###git add (-A) *file_name*

git add is used to add files to the next commit

here is the syntax:

user@computername /C/path/to/your/TestRepo (master)
$ git add file_name  

PROTIP:

  • one can use TAB to complete file names here too
  • using the flag -A will add all modified, changed, and deleted files to the next commit
    • An example:

user@computername /C/path/to/your/TestRepo (master) $ git add -A
```

###git reset *file_name*

git reset will remove the file from the current commit

some syntax and output:

user@computername /C/path/to/your/TestRepo (master)
$ git reset file_name
Unstaged changes after reset:
M 		README.md

###git commit (-m '*the message*')

git commit commits the changes, modifications, and deletions of your files followed with a message description of the alterations to your local git repository

For git commit, it is better to use the -m flag and just type your commit message in the command line

syntax and output of a commit:

user@computername /C/path/to/your/TestRepo (master)
$ git commit -m'test commit'
[master k512aoe] test commit
 1 file changed, 1 insertion(+)

###git log

git log shows a list of commits in descending order (displays scrollable less style viewer if the list of commits extends beyond the height of the terminal window

  • remember, press q to quit the viewer)

###gitk

gitk provides a better graphical interface to replace git log. It shows a list of commits as well as branching (which we will get into later).

##Pushing and Pulling Basics

###git push (-u) (*remote_repo_name* *remote_branch_name*)

git push will take your local repository and push it to the remote repository specified (in this case Github).

Syntax:

  • for the first commit:

user@computername /C/path/to/your/TestRepo (master) $ git push -u origin master ```

The `-u` flag tells git that the local branch should track the remote branch for changes and to set *origin master* as the default remote branch to push to.
  • for the second (and later commits):

user@computername /C/path/to/your/TestRepo (master) $ git push ```

Since the branch already has a default remote branch to push to, it will automatically push to origin master in this case.

Errors:

  • An error may arise stating that there are changes on the remote and that the local is not up-to-date. In this case perform a git pull, or more recommended with more complex problems: a git fetch, diff, and merge as shown in the next part to update the local branch before pushing once more.

###git pull (*remote_repo_name* *remote_branch_name*)

git pull pulls files from the remote repository and merges them with your local files.

IMPORTANT NOTE

  • Remember that these are the basics to pushing and pulling, and pulling without first fetching and diffing (discussed in the next part) should never be done in practice. This is because git may accidentally overwrite some of your changes when resolving differences between your local repository and remote repository.

Syntax:

  • general:

user@computername /C/path/to/your/TestRepo (master) $ git pull ```

  • if default remote repository has not been specified

user@computername /C/path/to/your/TestRepo (master) $ git pull origin master ```

##A Stereotypical commit Process

user@computername /C/path/to/your/TestRepo (master)
$ git add -A

user@computername /C/path/to/your/TestRepo (master)
$ git commit -m'test commit'
[master k512aoe] test commit
 1 file changed, 1 insertion(+)

user@computername /C/path/to/your/TestRepo (master)
$ git push

##Resetting

⚠️ **GitHub.com Fallback** ⚠️