Skip to content

Useful Git Commands

jhasenzahl edited this page Jun 15, 2012 · 12 revisions

Commands

Making Changes
git status
List changes since last commit.

git add .
Tracks any untracked files, and stages all modified files for the next commit.

git rm file
Deletes file locally and stages it for removal in the remote. Just using rm will not remove the file from your remote repository unless you run the command below.

git add . -A
Same as git add ., plus deleted files in local repository will also be deleted on the remote (if you used rm instead of git rm).

git commit -m "Message"
Commit all staged files, if git add was used.

git commit -a -m "Message"
Automatically stage and commit all modified files. Unlike git add ., will not track untracked files.

git commit --amend
Amends the last commit with any new staged files. Useful if you forgot to include something in your most recent commit.

Branching
git merge branchname
Merges branchname into the current branch.

git push
Pushes all commits of the current local branch to the remote branch of the same name.

git branch
Lists all local branches.

git branch branchname
Creates local branchname

git branch branchname origin/branchname2
Creates local branchname tracking remote branchname2 (they usually will have the same name).

git branch -d branchname
Deletes local branchname.

git branch -D branchname
Force deletes local branchname. Use with caution, may result in data loss.

git checkout branchname
Switch over to branchname.

git pull
Get any changes from the remote and sync with your local repository.

Tagging
git tag
Lists all current tags.

git tag -a tagname -m "Description"
Creates an annotated tag.

git push origin tagname
Syncs tagname with the remote.


Example Usage

Merge a finished feature to develop
git checkout joe-dev: Switch to joe-dev branch
[do stuff here]
git add .: Add all modified files to next commit
git commit -m "Implemented feature X": Commit all added files
git push origin joe-dev: Sync local commits with the remote branch joe-dev
git checkout develop: Switch over to the develop branch
git merge joe-dev: Merge the new stuff from joe-dev
git push origin develop: Sync local merge with the remote branch develop

Then, if you want to mark this as a milestone
git tag -a v0.7 -m "Implemented feature X": Create annotated tag v0.7
git push origin v0.7: Push tag v0.7 to the remote


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Clone this wiki locally