Some basic Git commandsOctober 11, 2013

This is a quick-start guide that details some basic Git commands for version control

Using Git locally and then checking out branches, merging them and reverting changes

You can download Git for Windows here if you just want a version control system you can use to make changes to your own projects that you can easily merge or revert if disaster strikes. Once installed you will be able to use it via either a command-line interface or a graphical user interface.

Git Bash on Windows

Navigate to your project’s directory

$ cd ~/myproject

Add the project files to git

$ git init

Stage the files

$ git add *

Do the first commit

$ git commit *

Make a new branch that will be completely separate from yet completely the same as the master branch, so that you can make changes to your project freely

$ git branch test

Checkout the branch you want to be using

$ git checkout test
Switched to branch ‘test’

To see a list of branches (the starred one is the one you’re currently using):

$ git branch
* test

Change some files… To see which files have been changed, what the changes were, etc:

$ git diff

To see which files have been changed and which need committing:

$ git status

Stage the files you want to commit

$ git add *

Commit the changes

$ git commit -m “Edited some files in this test branch”
[test 3af41dc] Edited some files in this test branch
1 file changed, 1 insertion (+)

To merge this branch with the master branch, first change to master branch, then merge the branches

$ git checkout master
$ git merge test

To see which files have been changed recently, the commit messages and dates

$ git log

To see the last commit, and what files were changed and finer details of the changes

$ git show

To revert changes to a file

$ git checkout –filename

To delete a branch entirely

$ git branch -D test
Deleted branch test (was 542b809).

Github – edit and share yours (or others) code publically

Once you’ve set up your free Github account, then downloaded and installed Github’s Git Shell from here, Find and run Git Bash from Start Menu

Make a directory for the project (in your User directory)

mkdir ~/New-Project

Change Directory into the project’s folder

cd ~/New Project

Run Git initialisation in the folder

git init

Add your project files to C:/Users/Username/New-Project

‘Stage’ your project files – (this means they will be included in the next commit)

git add *

‘Commit’ your files – (you’re confident with all your changes) and add an optional message

git commit -m ‘Committing some files to the remote repository, do dah, do dah’

Add the ‘origin’ if you haven’t already – (the origin is the remote site, the master is the local)

git remote add origin

On Github, add the repository ‘New-Project’. (Also, add a license or README file or else it won’t actually be created.)

You can’t push files yet because files exist on the origin that don’t exist on the master – the license or README file. Pull them down to the local repository:

git pull origin master

Then ‘Push’ the files up to the origin from the master

git push origin master

Category: Cheat SheetsTutorials

Leave a Reply

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