Git Commands

From Exterior Memory
Jump to: navigation, search

The Git commands are in particular useful with GitHub

Download Code

Download (fork) a remote repository

git clone remote-url
git clone git@github.com:macfreek/some-project.git

Add a second remote site as a branch

git remote add branchname remote-url
git remote add camblor https://github.com/macfreek/some-project.git

This will create branches under the macfreek/ prefix, e.g. macfreek/origin, macfreek/v1.4 etc. You need to git pull --all to download the new branch.

Download (fork) a remote subversion repository

git svn clone --stdlayout url
git svn clone http://tvgrabnlpy.googlecode.com/svn

Fetch updates from a remote repository

git fetch
git fetch origin
git fetch --all

Fetch and merge updates from a remote repository

git pull master
git pull origin master
git pull --all master

Fetch updates from a remote subversion repository

git svn fetch

Track a remote branch

git branch -t branchname origin/branchname

Upload Code

Upload committed changes to a remote repository

git push
git push origin

Upload to a different remote repository

Push the local "upstream" branch to the "master" branch at repository "remote":

git push remote upstream:master

Upload committed changes to a remote subversion repository

git svn dcommit

Upload tags to a remote repository

git push --tags origin

Upload to multiple repositories at the same time

See: http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations

Create a Remote Repository

Set up an empty (bare) remote repository

mkdir new-project.git
cd new-project.git
git init --bare
git-update-server-info

Start a local repository

mkdir new-project
cd new-project
git init
[create some files or folders]
git add *
git commit -m "My initial commit message"

Initial push to remote repository

git remote add origin git@example.net:new-project.git
git push -u origin master

Delete Remote Code

Delete a remote branch

By default branches are not deleted from a remote site, even not with a git push --all. Use --delete or the colon (:) prefix to delete them explicitly:

git push --delete remotename branchname
git push remotename :branchname
git push origin :branchname

Delete tags from a remote repository

git push origin :refs/tags/12345

Showing Status

Status

Show status:

git status

Code Diffs

Show diffs between working directory and branches. (The index is the staging area to create commits. See The four buckets — how Git considers content for an explanation the concepts) (Image courtesy Abizer Nasir)

GitDiff.png

Commits

Show recent commits:

git log
git log --oneline
git log --oneline --graph

Show tags:

git tag

Branching

Create new branch

git branch branchname

To create a branch and check it out:

git checkout -b branchname

Select a branch to work on it

git checkout branchname

Delete a branch

git branch -D branchname

Rename a branch

git branch -m oldname newname

Stashing

Stashing is the process to put aside some changes for later re-use. This is used if you are working on code, but some bug report comes in that you like to work on. In that situation, you probably don't want to commit the code you are working on, but you want to stash it.

To create a stash:

git stash save Name-of-stash

To list and delete a stash:

% git stash list
stash@{0}: On master: creating example files
stash@{1}: WIP on master: 06117fb Catch exceptions for unknown values
% git stash drop "stash@{1}"
Dropped stash@{1} (a856cb1e08986b0d366a8623409b0daf7f2ea496)

To reapply a stash:

git stash apply "stash@{1}"

Branch Manipulation

Create multiple pull requests (in GitHub)

Imagine you have made four commits, and want to create two separate pull requests. In this example branch topic1 contains commits B and C, while branch topic2 should only contain commits D and E.

A upstream/master -- B -- C topic1 -- D -- E topic2

Making a pull request for topic1 works as expected, but a pull request for topic2 will contain commits B, C, D and E. Not just D and E.

The solution is to create a new branch and to cherry-pick commits D and E:

git branch -b topic2_req upstream/master
git cherry-pick sha1_D sha1_E
git push origin topic2_req

The history will now look like:

A upstream/master -- B -- C topic1
                 \
                  \-- D -- E topic2

Move branch pointer to a different commit

The normal action is to simply delete the old branch, and create a new branch at the desired commit:

git branch -D branch-name
git checkout commit-sha1
git branch branch-name

However, if you want to move the master branch pointer, it may be useful to do the following:

git branch -f branch-name commit-sha1

Undo

Fixing uncommitted changes

Anyone makes mistakes. If you have not committed your code, you can simply edit the files and add them (again).

Even after you committed a file, you can undo your changes. A word of warning though: if you undo a commit, you change the history of your repository. If you are deleting a commit that you or someone else used as the basis of further changes, you screw up that work. So only undo a commit if you have not done anything else yet (changed code, pushed it to a remote repository, etc.)

Further reading: Git Basics - Undoing Things in the Git book.

Fixing accidentally deleted files

To fix an accidentally deleted file, revert it by checking out the version of the previous commit:

git checkout -- deletedfile
git checkout HEAD -- deletedfile

Unstaging a File

To remove a file from the index:

git reset HEAD -- filename

Amending the Commit Message

If you just created a commit, and noted a typo in your commit message, you can fix this as follows. Be aware that you change history. Don't do this if you already pushed the commit to a remote repository.

git commit --amend

Amending Committed Files

If you just created a commit, but note an error in one of the files, you can add the changes to the index and redo the commit. Be aware that you change history. Sometimes it is better to simply make a new commit with the fix.

git add changedfile
git commit --amend -C HEAD

(the -C HEAD re-uses the commit message of the existing commit)

Undo a Local Commit

git commit --amend works by undoing a commit, and redoing it. To only undo the commit, use git reset. there are three options.

To only undo the commit, leaving the changed files in the index:

git reset --soft HEAD~1

To undo the commit, leaving the changed files in the working tree:

git reset HEAD~1

To undo the commit, deleting the changed files:

git reset --hard HEAD~1

Abizer Nasir created the following overview picture:

GitBuckets.png

Further reading: Undo last Git commit at Stack Overflow, in particular the second and fourth answers.

Undo a Remote Commit

It is possible to undo a commit, even after it has been pushed to a remote repository, but a better solution is to make a new commit that reverts the old commit:

git revert HEAD

If you really want to remove a previous commit from a remote repository, change the branch pointer, and push that branch:

git reset HEAD~1
git push --all

Again, this is bad practise.

Finding Deleted Branches

If you somehow changed your branch pointer to a different commit, leaving some previous commits dangling, those commits are no longer shown by git. Thankfully, there is still a way to get those commits back.

List unreachable commits:

git reflog
git log -g

Find even more unreachable commits and other blobs:

git fsck

If you find the commit you are looking for, make a new branch called recover-branch for this commit:

git branch recover-branch sha-of-unreachable-commit

You can now manipulate this branch-as any others, and doing merges, cherry-picking, etc.

Further reading: Git Internals - Maintenance and Data Recovery in the Git book

Cherry-Picking

Add a third-party commit without a pull request

git cherry-pick -n sha-1
git commit -c sha-1

This will preserve author information.