Skip to content
Kenny Yu edited this page Oct 23, 2013 · 2 revisions

Basic git workflow

We'll walkthrough a sample workflow of how to use git.

git status

You edit some files foo.c and bar.c in your local directory. When you save these changes, these changes are reflected in your local file system, but git has not snapshotted these changes yet. To display a list of files that you have changed:

git status

git status

git diff

To view the changes in your local file system: git diff. To view the changes in a specific file, use git diff FILE

git diff

git add FILE

Next, we want to add the changes in our files to the staging area to prepare our commit. To add a file to the staging area, us git add FILE. To remove a file from the staging area, use git reset FILE.

git add

git diff --staged

To view the changes in the staging area, use git diff --staged. Use this to actually see what changes are actually going into your commit.

git commit

Next, let's take a snapshot of these changes and save it into our history.

git commit -m "COMMIT MESSAGE

git commit

That will take all the changes from the staging area and snapshot them into our local repository. Running git commit without the -m option will open the default editor (typically vim) and allow you write a commit message.

It is good practice to write descriptive commit messages to help you and other people on your project understand what changes are in the commit.

git log

View the history of all your changes. To see a prettier version, use:

git log --graph --oneline --branches --decorate --all

git log

git push REMOTE_NAME BRANCH_ON_REMOTE

Note that your local repository and remote repository typically each have a branch called master, but they are not necessarily in sync with each other. For example, you may have commits on your local machine that you have not yet pushed to github. To sync your changes with a remote repository (e.g. your github repository), use:

git push origin master

git push

This command will take all the commits on your current local branch (typically master, but doesn't have to be. To see what branch you are on, use git branch) and push them to the specified branch on the remote (in this case, we push to the master branch on the remote called origin).

git pull REMOTE_NAME BRANCH_ON_REMOTE

If you are working with other people on the same repository, they may have pushed commits to the shared remote repository. To fetch these changes and merge them with the commits you currently have on your machine:

git pull origin master

git pull

This will (1) fetch any changes from the specified branch (in this case master) on the remote (in this case origin), and then (2) attempt to merge these changes with commits on your current branch.

This command may result in merge conflicts. We'll have an exercise later to resolve merge conflicts.

NOTE: git pull origin master is actually shorthand for these commands:

git fetch origin        # fetch new commits from the remote, but don't merge
git merge origin/master # attempt to merge the origin/master branch with the current branch

Recap

  1. git branch What branch am I on? Typically master.
  2. Make changes to files
  3. git status What files have changed?
  4. git diff What are my changes?
  5. git add FILE Prepare a file to be committed by adding a file to the staging area
  6. git reset FILE If you want to remove a file from the staging area
  7. git diff --staged What changes will be committed?
  8. git commit -m "commit message" Snapshot these changes into history. Use git commit --amend to edit an existing commit that you have not pushed yet.
  9. git log View history. More useful: git log --graph --oneline --branches --all --decorate. We'll show you how to setup an alias for this long command in a bit.
  10. Repeat 2-9 several times, making several commits.
  11. git pull origin master Fetch any changes from the master branch on the origin remote repository, and merge them with the commits on my local branch.
  12. Fix any merge conflicts.
  13. git push origin master Push all my local commits from my current branch to the master branch on the origin remote repository.

Finish Bootcamp

Go back to the main page.