Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Mitchell Fargher
Mitchell Fargher
11,634 Points

Using git practically

I am a bit confused with using git and github. The videos on how to use it were very helpful and the way they used it made sense, but I'm struggling with how to use it practically. Do you do your normal work INSIDE git like in the tutorials or do you work in your normal text editor and then just find the file inside of git and commit it.

This was confusing me sense they just showed it from inside of git and I really would prefer working in an editor (brackets) and pushing the file I created from there.

Thanks!

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Mitchell,

Yes this confused me a lot too. I'm sure there are some users who will chip in soon with the most basic commands that you will need but you are right.

  • You initialise a local repository with your Git command line
  • Git files track changes in your repository (or folder) behind the scenes
  • You check the status of these changes regularly.
  • You "stage" the files before commiting the changes.
  • Commit early and commit often
  • check a log of your comitts

There is so many cool things you can do with git. I love how with git you can go to a previous version of your files (so long as you have a commit for it) and go back and make some changes to your files. Once done you then move those changes to another "branch", knowing that your changes are save.

But to emphasize again you absolutely do save and work with your files in your editor as normal and then save (or commit) those changes in Git so you have a tracked version of those changes behind the scenes.

Mitchell Fargher
Mitchell Fargher
11,634 Points

So any changes made in my text editor or inside of git will reflect in the other automatically? Will I be able to see the repository folder I make on my computer so I know where to place my files?

It just seems like there is a disconnect between the two since I've never used it before haha. Also, can this be done directly through GitHub or does it just host the files that you push through the remote ?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

You'll be able to move around folders using your command line. What system do you use for development? Have you used a command line too before?

To initialise a repository you use

git init

You'll then see the name of the default branch (master) listed at the end of your location in the command line. Ince you see that, you know you're working in a repository that Git is tracking.

You can use the GitHub GUI app to do this too but there's a difference between a local repository that works on your computer and a remote repository hosted on Github. :-)

Mitchell Fargher
Mitchell Fargher
11,634 Points

I use a Windows laptop and downloaded bash for it. I have used a command line and creating the folder and files inside of git I do understand. It's the outside of git that I'm not wrapping my head around.

Say, I'm working on a file saved in a folder called "website" on my desktop. Inside "website" I have index.html and style.css. Say I add a bunch of markup to both of these files using my text editor brackets. At this point, what do I do using git to commit these files I made/changed?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Okay it's at that point you first check the status of the files to see if they're being tracked.

git status

This should tell you all the files that have been changed or added and therefore need staging.

git add .

git add . is a shorthand command that will add all the files in one go but you can add them individually by a specifiying each file name.

git add index.html style.css profile.css mission.css

Once done the files are ready to be committed

git commit -m "message that goes with commit"

Commit early and often. Every change you think is significant should be committed to your local repository.

You can then even go back to a particular point by "checking" to a particular commit.

git log

Take a note of the checkout code that permits to the commit to you want to go to and type something like this

git checkout 54gfdf465

And then watch as the file changes before your very eyes. If say you deleted a file and want it back you could go back to that commit and start again. Very powerful stuff.

Mitchell Fargher
Mitchell Fargher
11,634 Points

Ah great that was very helpful!

When inside git though how do I find those specific files I worked on? How do I navigate so that git knows those files in the "website" folder on my desktop are the ones to look for? Does git status find every single file on your computer that changed and you just have to add the correct one?

Katherine Marino
Katherine Marino
3,286 Points

To get to the correct folder in your command line: the command to navigate to a new folder is "cd". So for example, you can do

cd C:\Dev\website

Or whatever the full filepath to your project folder is. Your command line should change so the prompt looks like

C:\Dev\website>

which is how you can tell what folder your command line is currently in.

You can also navigate relative to your current folder by using partial file names. So if you were in C:\, you can type

cd Dev

to move into the Dev folder. "cd .." will back you out to the parent directory.

You would make sure you were in the correct folder before using the "git init" command the first time. Once you navigate here with the git shell and have a repository initiated, the prompt will also show a little info about which tracked files have been changed and the name of your branch. Then you can use the commands Jonathan lists above.

I'm not sure if there's a lesson on it on here, but I would encourage you to go through a tutorial on using the command line if you're not familiar with it - it's a lot nicer to use once you're more comfortable with it and know what's going on!

Mitchell Fargher
Mitchell Fargher
11,634 Points

Katherine,

That was exactly what I needed! So once I navigate to the correct folder and initiate the repository I will then have the git set up IN the folder I'm working in to consistently commit the files from that folder

Kevin Korte
Kevin Korte
28,149 Points

Sounds like you got it now Mitchell,

I was going to say, each new project you start, you would want to run git init at the root of the projects folder. This sets up a new git project, and this git project will track everything in the folder alongside it where git init was ran, as well as any new or additional files and folders you add inside this project.

From here, you can work in your text editor as usually, and git will quietly track everything that is new. Brand new files and folders will initially be untracked. Modified files and folders will be tracked, but changed. You can add all the changes, or you can add only some. Once files are added, you can commit them. All of this is done separate of Github. Git and Github are actually very separate. It's when you push them Github or Bitbucket is when you are making the changes available on the internet. Technically Git itself keeps everything stored on your local computer.

One more thing to note, there will be a time when you want to store secrets. Things people like me shouldn't be able to see, that your application needs. Passwords, API keys, etc. You wouldn't want to commit your username and password to your sendgrid account.

Usually, you store those secrets in a single file together, and load them as environment variables, or some other means. Than you use or create a file called .gitignore in the root of your project, and add any files or folders that you don't want the internet to see. Anything in .gitignore will be completely ignored by Git, and can't be checked in for a commit.