Commands For NewGits

Here are a number of the essential command line git commands that can be used for working on shared repos in corporate environments. I use a combination of commandline and visual tools to manage my git repos and source code, and find it the perfect combination of power, speed and capability.

In a bind (or on a remote server via ssh) you can easily get by with just the commandline.

Assumptions and Prerequisites

  • You have a local repo or a central bare repo and a number of developers pulling and pushing to the central repo.
  • You have the commandline version of git installed on your corporate windows laptop/desktop. Don't worry if you are languishing on XP on a large corporate or government client network, git will work there very nicely indeed.
    • If you are on linux use your package manager to pull down the latest version of git (e.g. on Ubuntu use sudo apt-get install git-core)

git init

Initialises a git repo in the current directory.

git clone

Clones an existing repo into a new directory.

git status

Details the status of the repo, eg what has been added, modified, and whether changes are staged for committing.

git add filename

Adds a new file and stages it. Stages an existing file that has been modified.

git add -u

Recursive add of modified files.

git add *

Recursive add of files not yet added.

git commit -m "your commit message"

Commits the staged changes to the current branch and repo. The m flag adds a comment that you provide in the quotes.

git branch

Lists the existing branches. This will also highlight your current branch from the list.

git branch newbranchname

Creates a new branch called newbranchname. To switch to this branch use the checkout command.

git branch -d branchtoodelete

Delete a local branch you no longer need

git remote

Lists the remote repos you have configured.

git remote -v

List the remote repos you have along with their key details.

git remote rm oldremote

Removes a remote

git remote add newremote git://newremotelocation.git

Add a new existing remote

git pull origin master

Pulls the changes to the master branch from the remote repo called origin.

git push origin master

Pushes your locally committed changes to the remote master branch on the origin repo.

git log

Lists the changes committed in chronological order.

git checkout yourbranch

Switches your working environment to the yourbranch branch.

git merge branchtomergefrom

Merges the changes from branchtomergefrom into your current branch.

git gc

Cleans up your local .git folder and recompresses the packs. May or may not reduce the total folder size on your dev machine.