Skip to content

git basics

vmagnin edited this page Sep 6, 2024 · 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 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 git@github.com:vmagnin/gtk-fortran.git
cd gtk-fortran
mkdir build
cd build
cmake ..
make

To show the configuration:

git config --list

To list the remote repositories:

git remote -v

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
git pull --all    # Pull all commits from all remote branches

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

To show all ignored files:

git status --ignored

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 branches and show the current one:

git branch -a    # all branches (both local and remote)
git branch -vv   # show the latest commit in each branch and remote branches
git branch --sort=-committerdate    # sorted by lattest commit

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)

Other stash commands:

git stash show    # Show what has been stashed
git stash pop     # Unstash
git stash apply   # Apply stashed changes
git stash list
git stash clear

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 --check    # Show files with conflicts
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
git diff commit1..commit2
git diff --color-words    # Diff at the words level, instead of lines
git diff --color-moved    # With a specific color for moved lines

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

To show all branches containing a commit:

git branch --contains e7dbb157

Undo

To undo something:

git checkout file.f90  # Revert file.f90 to the last committed version (to cancel modifications)
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

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
git tag --list

History

To view the commit history:

git log
git log -4         # Show the 4 last commits
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 (insertions, deletions) for each file
git log --pretty=oneline   # Show only the hash and the subject
git log --author=vmagnin
git log --after="2015-06-01" --before="2015-08-01"
git log --since=3.months
git log --grep "blabla"    # Show only the commits whose title corresponds to the regex
git branch -v      # Show last commit on each branch
git show 0a45f2b6  # Show the content of that commit
git log file.f90    # Show only logs concerning this file
git log -S "search a string in commits"    # Or use -G instead if it includes a reGex
git log --merges    # Show only the merge commits
git shortlog --since=10.months     # The commits are grouped by author
git shortlog --sn    # Statistics by author

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

To show the journal of commits and branches checkouts:

git reflog

Bisection

git bisect start
git bisect bad HEAD          # Lattest commit
git bisect good a1b2c3d4	 # A commit before the bug was present
git bisect bad    # Was the bug present or not at this commit?
git bisect good
git bisect reset  # When the search is ended

Maintenance

git gc    # Cleanup unnecessary files and optimize the local repository

Miscellaneous

git help
git help --guides

Back to Home

Clone this wiki locally