Everyday git commands

After updating or creating a file:

You’v created a file and want to update the repo.

git add filename.txt

Then commit the change:

git commit -m "This is a message you can post with the commit"

Now git knows that you want this change to update the git repo, but it still hasn’t pushed the change to git yet. It’s only local, to push it to git:

git push -u origin master

To start a new branch:

One thing that isn’t immediately clear to newcomers to git is the fact that you can track multiple branches in one directory on your server or local development environment. For instance, let’s say I have a site with two branches. I have a master branch (this is a production branch meaning I can take any instance of that branch and upload it to my server and expect it to work). Then I have a “develop” branch (this is the branch where I have all my ongoing local development going on). However, this doesn’t mean I need to have two separate directories that contain two different versions of my site. The beauty of git, is it allows you to tack changes made on different branches simply by switching from one to the other and working from the same local directory. I can merge changes to a master branch, upload the updated site to my server, expect it to run and then switch back to my development branch and start pushing updates immediately.

Let’s say we have a master branch and we want to start a development branch to work on, then we’ll merge with our “master” after we’re sure it’s production ready. First we create the branch:

git branch develop

Now we have a branch called “develop”, let’s check it out by typing:

git branch

We can see our return we get “*master” and “develop”, which means we’re still working on our master branch. To switch over, let’s type:

git checkout develop

Now we can commit our changes and switch back to the master:

git commit -a

If I want to push those changes to be store on the git servers for the development branch I could push them the same way I would push for the master branch:

git push origin develop

Now we can switch back to master:

git checkout master

Useful tips:

If at any point you wish to see some information in the command line about your history. You can view the log file:

git log

To get the status of a directory and see if you need to push any updates:

git status

When starting a new repo, make sure you create the directory locally and run:

git init

To add all the updates and get them ready for commit:

git add --all

Leave a Reply

Your email address will not be published. Required fields are marked *