Git workflow cheatsheet
Using the GitHub Mac app for committing and pushing the changes in your files to the remote repository is pretty easy, but what about doing it through command line, or the Terminal, in OS X?
Commint changes locally
-
Open the Terminal
-
Navigate to your local repository
Use the cd command. According to my folder structure, the command line would be
$ cd Documents/igorgaelzer.github.io
-
Check out the git status of the files
Use the command below in your terminal.
$ cd git status
You should get something like this:
×-+MacBook-Pro-de-Igor:igorgaelzer.github.io igorgaelzer$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes not staged for commit:
# (use "git add..." to update what will be committed)
# (use "git checkout --..." to discard changes in working directory)
#
# modified: unit1_projects/stylesheets/blog.css
#
# Untracked files:
# (use "git add..." to include in what will be committed)
#
# unit1_projects/week2_git_workflow.html
no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-de-Igor:igorgaelzer.github.io igorgaelzer$
So... what does that mean? In this case, it means that we have changes in two files.
The first one shown in red is a modified file. This means that it has already been tracked by git before and it's been modified since then.
The second line in red is under Untracked files. This means that this file is new and it has never been committed.
-
Add the files that you want to commit to the staging area
Before changes are committed to git, you have to add them to the staging area. Basically, you are telling what files git should consider when saving the changes in the project.
To add a file to the staging are, use the command git add.
$ git add unit1_projects/week2_git_workflow.html
Or, in case there are many modified files in the project folder and you want to stage all of them, you can just add the whole project folder at once like shown below.
$ git add unit1_projects
Now let's take another look at the files status
$ git status
You should get something like this:
×-+MacBook-Pro-de-Igor:igorgaelzer.github.io igorgaelzer$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# (use "git reset HEAD..." to unstage)
#
# modified: unit1_projects/stylesheets/blog.css
# new file: unit1_projects/week2_git_workflow.html
Now, we see that both new and modified files are shown in green and under Changes to be committed. Thats a good sign!
-
Committing the files waiting in the staging area
Now we are ready to committ the changes and save this version of the project to git. This way, we can roll back to it later if needed.
Use the command git commit -m 'message goes here'.
In my case, as I'm making a change in my blog's css stylesheet so I'm going to use "Changed text colors in the blog`s stylesheet" as the message. This is an important step for identifying this commit in the future in case I want to roll back.
$ git commit -m 'Changed text colors in the blog`s stylesheet'
The -m command let's the Terminal know that the text after it is a message. Once you are done you should see a message like this:
×-+MacBook-Pro-de-Igor:igorgaelzer.github.io igorgaelzer$ git commit -m 'Changed text colors in the blog`s stylesheet'
[master 92f5ad7] Changed text colors in the blog`s stylesheet
2 files changed, 701 insertions(+)
create mode 100644 unit1_projects/week2_git_workflow.html
It says that 2 files were changed and that is what we were expecting!
Pushing files to the remote repository
It is a great idea pushing your changes to a cloud service like GitHub so you can work with a remote team, share your code, get help on it, access it from anywhere and have an extra backup on the cloud.
-
Make sure you have GitHub installed
If you are not sure, check out this step-by-step article on how to do it.
-
Push the changes
Make sure all changed files are in the staging area and simply use the git push command.
$ git push
Then once you are done you should see something like this:
×-+MacBook-Pro-de-Igor:igorgaelzer.github.io igorgaelzer$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Everything up-to-date
If the last line says Everything up-to-date we should be good!
Fetching changes in the remote repository
What I`m going to do now it fetching the changes in the phase_0_unit_1 repository - the one that has our DBC weekly instructions - to my local and remote repository so I can work on the latest verion.
-
Make sure you are in the right repository
You should be in the phase_0_unit_1 directory, which is the one we are going to fetch.
-
Check out your current remote repository
Just type in
$ git remote -v
and you should get something like this
×-+MacBook-Pro-de-Igor:phase_0_unit_1 igorgaelzer$ git remote -v
origin https://github.com/igorgaelzer/phase_0_unit_1 (fetch)
origin https://github.com/igorgaelzer/phase_0_unit_1 (push)
Alright! Looks like my remote repository is on my own GitHub account.
-
Set up a new remote repository
What we want to do now it to fetch (grab changes) made by Dev Bootcamp. This is why we have to set DBC GitHub repository as the remote.
To do that, type in the code below in the terminal:
$ git remote add upstream https://github.com/Devbootcamp/phase_0_unit_1.git
Great! Now let's make sure we have set the new remote repository correctly.
$ git remote -v
You should get something like this
×-+MacBook-Pro-de-Igor:phase_0_unit_1 igorgaelzer$ git remote -v
origin https://github.com/igorgaelzer/phase_0_unit_1 (fetch)
origin https://github.com/igorgaelzer/phase_0_unit_1 (push)
upstream https://github.com/Devbootcamp/phase_0_unit_1.git (fetch)
upstream https://github.com/Devbootcamp/phase_0_unit_1.git (push)
-
We are now ready to fetch the changes!
It's finally time to grab the changes made in the DBC`s repository. This should bring the changes to a special branch in our local repository and it`s important to know that they are not merged yet.
$ git fetch upstream
You should get something like this
×-+MacBook-Pro-de-Igor:phase_0_unit_1 igorgaelzer$ git fetch upstream
From https://github.com/Devbootcamp/phase_0_unit_1
* [new branch] final-033014 -> upstream/final-033014
* [new branch] final-04202014 -> upstream/final-04202014
* [new branch] master -> upstream/master
* [new branch] revamp_week_1 -> upstream/revamp_week_1
The fetched files will be stored in your local branch called upstream/master, waiting to be merged with your files. If you want to see these files you can use the command below.
$ git branch -va
-
Merge the changes
First, make sure you are on your master branch.
$ git branch
or
$ git checkout master
Now let's merge the changes. Don't worry, this won't make you lose the changes you made on your own files.
$ git merge upstream/master
-
Commit and sync your new local changes to your own remote repository
Now it's time to get these new updated files to your own GitHub account in the cloud.
$ git commit -m "added Devbootcamp's changes"
and then
$ git push origin master
Origin refers to your local repository and master refers to the master branch on your github.com forked repository.
Your new updated files should now be in your github.com account!