Git Command Guide

 

Lesson 1

Initializing git project:

$ git init

Three parts of git workflow:

  • Working Directory
  • Staging Area
  • Repository

Checking status of changes:

$ git status

Adding files to staging area:

$ git add filename

Checking the differences between the working directory and the staging area:

$ git diff filename

Storing changes from the staging area inside the repository:

$ git commit -m "commit message"

Standard Conventions for Commit Messages:

  • Must be in quotation marks
  • Written in the present tense
  • Should be brief (50 characters or less) when using -m

Viewing commits stored chronologically in the repository:

$ git log

You can press q to quit

Lesson 2

Viewing head commit (the commit you are currently on, mostly the most recent commit):

$ git show HEAD

Restoring the file in working directory from the last commit:

$ git checkout HEAD filename

Adding multiple files into the staging area in one command:

$ git add filename_1 filename_2

Unstaging files from the staging area

$ git reset HEAD filename

Rewinding to previous commit:

$ git reset (the first 7 characters of the SHA)

Lesson 3

Viewing all branches:

$ git branch

Creating new branch:

$ git branch <new_branch>

Switching to new branch:

$ git checkout <new_branch>

Merging the branch into master: Merge conflict occurs when you commit different changes in both new branch and the master.

$ git merge <branch_name>

Deleting a branch:

$ git branch -d <branch_name>

Lesson 4

Cloning a remote repository: Remark: Git will give the remote address the name origin

$ git clone <remote_location> <local_clone_name>

Viewing a list of a Git project’s remotes:

$ cd <local_clone_name>
$ git remote -v

Viewing if changes have been made to the remote and bring the changes down to your local copy:

$ cd <local_clone_name>
$ git fetch

Integrating origin/master into your local master branch:

$ cd <local_clone_name>
$ git merge origin/master

Now that you’ve merged origin/master into your local master branch, you’re ready to contribute some work of your own. The workflow for Git collaborations typically follows this order:

  1. Fetch and merge changes from the remote
  2. Create a branch to work on a new project feature
  3. Develop the feature on your branch and commit your work
  4. Fetch and merge from the remote again (in case new commits were made while you were working)
  5. Push your branch up to the remote for review

Steps 1 and 4 are a safeguard against merge conflicts, which occur when two branches contain file changes that cannot be merged with the git merge command. Step 5:

$ git push origin <local_clone_name>