Thursday, January 20, 2011

Git useful commands

Pulling changes

Pulling updates from master:
$ git pull
or
$ git pull origin master


Pull updates from arbitrary remote repository:
$ git pull repository.name.git HEAD

Pushing changes


Committing and pushing changes to remote branch (example, tlb branch)
$ git add filename
$ git commit
$ git push origin tlb

Merging


$



Commit and reverting files



Commit all tracked files:
$ git commit -a


Revert a single file:
$ git checkout filename

If the filename is the same as the branch name that could be a problem. In this case type:
$ git checkout --filename

Undo uncommitted changes for the whole repository:
$ git reset --hard HEAD


Adding a file to a previous commit. Suppose that you forgot to include a file in your commit. You can include this file by first adding it and then using the amend option.
$ git add filename
$ git commit --amend -C HEAD
The -C option will allow you to reuse the old message from the HEAD commit.




Adding and Tracking files

Staging all tracked files
$ git add -u

Untrack a file and remove it from the directory tree:
$ git rm filename

Untrack a file without removing it from the directory tree:
$ git rm --cached filename


List tracked files, or check if a single file is tracked:
$ git ls-files
$ git ls-files filename

Branches

List all branches:
$ git branch -a


Create a new branch:
$ git checkout -b branch_name

Create a new branch from an existing remote branch:
$ git checkout -b newbranch origin/branch_name

Change branches:
$ git checkout branch_name

Delete a branch
$ git branch -D <branch_name>


Rename a branch:
$ git branch -m <old_name> <new_name>

Diff between two branches:
$ git diff branch1..branch2

Merging

Configurations

Ignoring certain file extensions, names, or directories. To do we need to edit the .gitignore file. Example of a .gitignore file:
*.swp
*.pyc
log/




No comments:

Post a Comment