Skip to content

git basics

Vincent Magnin edited this page May 3, 2023 · 44 revisions

Back to Home

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

Most useful git commands

Configuration

To configure git, clone the gtk-fortran remote repository and build the default branch (--global means you use the same identity in all repos):

git config --global user.name "My name" 
git config --global user.email "My@email"
git clone [email protected]:vmagnin/gtk-fortran.git
cd gtk-fortran
mkdir build
cd build
cmake ..
make

To show the configuration:

git config --list

To see the settings of the remote repository:

git remote show origin

Updating the local repository

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 rebase history:

git rebase name_of_the_branch

Files

To delete a file:

git rm file.f90

To rename a file:

git mv oldname newname

Branches

To work on a branch:

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

To list all branches (both local and remote) and show the current one:

git branch -a

To create a new branch:

git branch newone        # without checkout to this branch
git checkout -b 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 (nothing before the ":"):

git push origin :name_of_the_branch

To rename a branch, locally and remotely:

git branch -m oldname newname
git push origin :oldname
git push --set-upstream origin newname

To merge a "foo" branch with the current branch:

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

To apply to the current branch one or more commit(s) from another branch:

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

Before changing branch without committing current branch:

git stash         # Stash uncommitted work (same as git stash push)
git stash list
git stash show    # Show what has been stashed
git stash pop     # Unstash
git stash apply   # Apply stashed changes

Stage

To list files in the git index:

git ls-files

To stage a file:

git add file.f90

To unstage a file:

git restore --staged file.f90

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

git rm --cached file.f90

Diff

To obtain the differences:

git diff            # Changes not yet staged
git diff --staged   # Changes that will be in the next commit
git diff gtk3 gtk4  # Diff between two branches
git diff gtk3 gtk4 -- cfwrapper.py   #  Diff in a file in two branches
git diff gtk3:examples/gtkhello2.f90 gtk4:examples/gtkhello2.f90   # another solution

Commit

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    # push a new branch on GitHub

To merge the last two commits:

git rebase -i HEAD~2
=> in the editor, replace the second "pick" word by "fixup"

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

git checkout -- *.f90 *.csv

Undo

To undo something:

git commit --amend   # Replace the last commit (just the message if nothing changed)
git revert 9fceb02   # Undo a commit (creates a new commit)
git reset HEAD file.f90   # To unstage the file.f90
git reset e7dbb157        # To undo the last commit and go back to the previous one e7dbb157
git checkout -- file.f90  # Revert file.f90 to the last committed version

Tags

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

History

To view the commit history:

git log
git log --oneline  # Short display
git log --graph    # To see branches graphically
git log -p         # Show the diff (patches)
git log --stat     # Show the stats
git log --pretty=oneline   # Show only the hash and the subject
git log --author=vmagnin
git branch -v      # Show last commit on each branch
git show 0a45f2b6  # Show the content of that commit

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

Clone this wiki locally