Jump to content

User:CaritasUbi/Git how-to

From Wikipedia, the free encyclopedia

This page consists of helpful hints from my own experience with Git.

Global configuration file

[edit]

To edit the global config file

[edit]
git config --global -e

Mine looks like this:

[user]
   name = My Name
   email = My.Name@myserver.com
[alias]
   log1 = log --pretty=format:\"%h - %an, %ad %ar : %s\" --date short -10

To create an alias from a command line

[edit]

For example, to create an alias for a short-form log:

git config --global alias.log1 'log --pretty=format:"%h - %an, %ad %ar : %s" --date short -10'

That log option creates output that looks like this:

42df7d8 - My Name, 2013-03-06 20 minutes ago : Explicit true causes MEH to be cleared
95ee118 - My Name, 2013-02-28 6 days ago : LuAnne's image
0fa3e90 - My Name, 2013-02-28 6 days ago : Moved to Eclipse project
7df80ac - My Name, 2013-02-28 6 days ago : captured sample timings for remaining time estimator
2efe9da - My Name, 2013-02-27 7 days ago : Renamed properties file
d368013 - My Name, 2013-02-26 8 days ago : Successful result of downgrade (test03 part C)
ece4f85 - My Name, 2013-02-26 8 days ago : First successful run of upgrade (test3 part b)
8b1c493 - My Name, 2013-02-26 8 days ago : Successful priming run
ad6a191 - My Name, 2013-02-26 8 days ago : Initial push of split scripts
bdad4eb - My Name, 2013-02-26 8 days ago : Failed test 3 part b

Reverting to another version of a file

[edit]

From the same branch

[edit]

To get a specific revision:

git checkout <commit-ref> -- <filename>

From another branch

[edit]
git checkout <branch-name> <filename>

A typical case is .gitignore


Branches

[edit]

Creating a remote branch

[edit]

After creating the branch locally:

git push -u origin <branch-name>

You can specify a different remote name other than origin

Getting a local tracking branch of a remote branch

[edit]

Get the remote branch name:

git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/stable
  remotes/origin/experimental

To then get access to stable, do this:

git checkout -t origin/stable

or, to give it a different local name:

git checkout -b myNewName origin/stable

Pulling all remote branches

[edit]

After you clone a project, you actually have references to all remote branches:

git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
  remotes/origin/v1.0-stable
  remotes/origin/experimental

You should periodically refresh the contents of these remote branches with

git pull

If you just want to take a quick peek at an upstream branch, you can check it out directly:

git checkout origin/experimental

But if you want to work on that branch, you'll need to create a local tracking branch:

git checkout -b experimental origin/experimental

Deleting a local branch

[edit]
git branch -d <branch-name>

If the branch has not been fully merged, you can force it with

git branch -D <branch-name>

Deleting a remote branch

[edit]
git push origin --delete <branch-name>

If this doesn't delete your local copy of the remote branch, do this:

git remote prune origin

Creating a branch retroactively

[edit]
git branch fixes       # copies master to new branch
git reset --hard XXX   # resets master to XXX (e.g., origin/master)

(from http://stackoverflow.com/questions/364925/is-it-possible-to-retroactively-turn-a-set-of-commits-into-a-branch)

Working with tags

[edit]

To see existing tags:

git tag -l

To tag a version (can do it retroactively with <commit>)

git tag -a -m <msg> <tagname> [<commit>]
git push --tags

To revert to a tagged version

git reset --hard <tagname>
git push --force # Only if you are sure

Undeleting a file

[edit]
git reset HEAD

Should do it. If you don't have any uncommitted changes that you care about, then

git reset --hard HEAD

should forcibly reset everything to your last commit. If you do have uncommitted changes, but the first command doesn't work, then save your uncommitted changes with git stash:

git stash
git reset --hard HEAD
git stash pop

Source: http://stackoverflow.com/questions/2125710/how-to-revert-a-git-rm-r