git basics

vmagnin edited this page Jul 17, 2016 · 30 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 git@github.com:jerryd/gtk-fortran.git
cd gtk-fortran
mkdir build
cd build
cmake ..
make

To update local repository from remote repository:

git fetch

To fetch and merge a local branch from remote repository:

git pull origin
git pull origin name_of_the_branch

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
git stash show    # Show what has been stashed
git stash apply   # Apply stashed changes

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 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

Before committing:

git diff --check   # Provide warning for trailing whitespaces

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
git tag -d v13.10   # Delete a tag (not pushed)
git push origin :refs/tags/v13.10   # Delete a tag on the server

To undo something:

git commit --amend   # Replace the last commit (just the message if nothing was changed)
git reset HEAD file.f90   # To unstage the file.f90
git checkout -- file.f90   # Revert file.f90 to the last committed version

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

git branch -a

To create a new branch:

git branch newone   # without checkout to this branch
git checkout -n newone   # create then checkout to the new branch

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 merge a "foo" branch with the current branch:

git merge foo
git mergetool   # to open a graphical tool to solve conflicts
git branch --merged   # to show branches already merged
git branch --no-merged   # to show branches not yet merged

To apply to the current branch a commit from another one:

git cherry-pick 9fceb02    # A new commit is automatically done

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
git branch -v    # show last commit on each branch

To graphically visualize the history and solve problems:

gitk

To see when each line of a file has been last modified and by who:

git blame file.f90

Back to Home