Skip to content

git basics

vmagnin edited this page Jun 12, 2016 · 51 revisions

Back to Home

A good online book: Scott Chacon, Pro Git, APress, a Creative Commons book. Click here for french version.

Most useful commands of git, the fast version control system:

To config git, clone the gtk-fortran remote repository and build the master branch:

git config --global user.name "My name"     # --global means you use the same identity in all repos. 
git config --global user.email "My@email"
git clone [email protected]:jerryd/gtk-fortran.git
cd gtk-fortran
mkdir build
cd build
cmake ..
make

To update local repository from remote repository:

git fetch

To work on a branch:

git checkout name_of_the_branch     # import the branch if it does not exist locally
git status

Before changing branch without committing current branch:

git stash

To update a branch from remote repository:

git pull origin
git pull origin name_of_the_branch

To add a new file in the index or to stage a file:

git add file.f90

To remove a file from the staging area and not track it:

git rm --cached file.f90

To delete a file:

git rm file.f90

To rename a file:

git mv oldname newname

To avoid committing some files (for example f90 and csv files):

git checkout -- *.f90 *.csv

To commit changes:

git commit   # Commit the staged files (open a text editor for the message)
git commit -a -m "message about the commit"    # stage and commit all the changes
git push origin name_of_the_branch

Commands concerning tags:

git tag v13.10   # Create a tag
git tag -a v13.10 -m "Version 13.10"   # Create an annotated tag
git tag -a v13.10 9fceb02   # Create a tag for an old commit 9fceb02
git show v13.10   # Show the tag data
git push origin v13.10    # Push the tag to the server
git push origin --tags    # To push all tags

To obtain the differences:

git diff   # Changes not yet staged
git diff --staged   # Changes that will be in the next commit
git diff master gtk3 -- cfwrapper.py   #  in a file in two different branches

To obtain the list of branches (local and remote) and know the active one:

git branch -a

To delete a local branch:

git branch -d name_of_the_branch
git branch -D name_of_the_branch    # Forcing

To delete a remote branch:

git push origin :name_of_the_branch

To view the commit history:

git log
git log -p   # and show the diff (patches)
git log --stat   # and show the stats
git log --pretty=oneline   # show only the hash and the subject

To graphically visualize the history and solve problems:

gitk

Back to Home

Clone this wiki locally