-
Notifications
You must be signed in to change notification settings - Fork 341
Basic git workflow
We'll walkthrough a sample workflow of how to use git.
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
To view the changes in your local file system: git diff
. To view the changes in a specific file, use git diff 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
.
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.
Next, let's take a snapshot of these changes and save it into our history.
git commit -m "COMMIT MESSAGE"
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.
View the history of all your changes. To see a prettier version, use:
git log --graph --oneline --branches --decorate --all
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
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
).
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
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
-
git branch
What branch am I on? Typicallymaster
. - Make changes to files
-
git status
What files have changed? -
git diff
What are my changes? -
git add FILE
Prepare a file to be committed by adding a file to the staging area -
git reset FILE
If you want to remove a file from the staging area -
git diff --staged
What changes will be committed? -
git commit -m "commit message"
Snapshot these changes into history. Usegit commit --amend
to edit an existing commit that you have not pushed yet. -
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. - Repeat 2-9 several times, making several commits.
-
git pull origin master
Fetch any changes from themaster
branch on theorigin
remote repository, and merge them with the commits on my local branch. - Fix any merge conflicts.
-
git push origin master
Push all my local commits from my current branch to themaster
branch on theorigin
remote repository.
Go back to the main page.