Log In | Users | Register
Edit | Attach | New | Raw | Diff | Print | | Tools
You are here: Data » DocTools » ToolsCtrl » Git » UsefulGitCommands



git config --global core.pager more

make your terminal interpret the escape sequences returned by the commands git status and git log to colorize their output (configuration, only needed once!)

git clone git@github.com:address-of-remote-repository

create your local copy of a remote repositore e.g. on github. 'Clone with ssh' protocol is the preferred method but first needs to be set up once.

Manage changes

git diff

show unstaged changes

git diff --cached

show changes that are already staged but not committed

git diff HEAD^

see difference relative to version before last commit (caret means "parent of", full details see here

git fetch

update your local copy of origin/master with the changes that have been made on the remote repository (without merging them)

git fetch remote-name branch-name

update your local branch branch-name from the remote repository remote-name

git diff HEAD origin/master

show changes from your working copy to origin/master (do a fetch first to update origin/master)

git diff HEAD origin/branch-name

show changes from your working copy to a remote branch branch-name (do a fetch of that branch first)

git status

see status information; after a fetch, see how much your master has diverged from origin/master

git show-branch master

see a list of commits that differ between your local master and the remote origin/master

git merge

merge changes from origin/master to your working copy

git pull

do git fetch and git merge in one step (if you want to update your local master without checking details)

Keep some files local

Files you don't want to go into the repository: put their names (you may use wildcards), one name per line, into a file named .gitignore Note that .gitignore can and in fact should be commited so that everybody that uses this repository ignores the same set of files (as for example *.o files or an obj directory).

Local files that you only want to exclude in your own working directory can be listed in the file in .git/info/exclude, see C2SM Wiki pages

Create branch

git checkout -b my-branch

create a new branch my-branch and switch to it, so that you can commit modification to this new branch (shortcut for git branch and git checkout, see below)

git push --set-upstream upstream my-branch

push your new branch to the remote repository upstream (usually origin, use git remote to see name) for the first time (use simply =git push= afterwards)

git branch my-branch

create a new branch my-branch (but stay on current branch)

git checkout my-branch

switch to (existing) branch my-branch

Note: Switching branches changes files in your working directory! Uncommitted changes may be overwritten.

git branch -a

see on what branch you are and what other branches are in your local repository

git log --decorate # add --online for shorter output on newer systems only

fancy way to see all existing branches

git checkout master

switch back to master branch (local edits remain)

git merge other-branch

merge changes from the branch other-branch to the current branch

git checkout -t origin/other-branch

checkout remote branch from remote repository origin and create corresponding local branch

git show-branch \*master

see a list of commits that differ for all branches of which the name ends on "master"

git branch -d branch-name

delete branch

Make a release

In the status line showing the numbers of releases, click on release, then click the "Create a new release" button. Releases should be merged back to C2SM? as backup. Do this in a clone rather than as pull request, to avoid the overhead of a merge commit. On escha/kesch, go to a clone (or make a clone) of the repository, fetch or pull the newest state including the release information.

Add C2SM? as additional remote: git remote add c2sm git@github.com:C2SM-RCM/your-repository.git

Push the current status to this remote: git push c2sm master

Push the release tag to this remote: git push c2sm release-tag

Mangage links to remote repositories and branches

git remote -v see origin of local clone, from where repository is fetched and to where it is pushed

git remote get-url origin show upstream repository

git remote update update your origin references with all changes on all remote branches (make a fetch for all branches on remote repository)

git remote set-url origin git://new.url.here change the upstream repository (origin is the default remote name, you may add different remote-names, see below)

git fetch -a copy information about all remote branches to your local repository

git remote add remote-name remote-address add another remote repository with the name remote-name and the address remote-address

git remote rm remote-name remove remote repository with the name remote-name from your local repository

Configuration tips

source ~kaufmann/.bash-git-prompt/gitprompt.sh

  • Git auto completion with tab key
  • Prompt showing current git status information (needs module load git)

Git-shell-tools (obtain information from git in shell scripts): https://github.com/C2SM-RCM/buildenv/blob/master/gitTools.sh

More about git

This site is managed by the Center for Climate Systems Modeling (C2SM).
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors. Ideas, requests, problems? Send feedback!