Notes about Git

From IdeaNet
Jump to navigationJump to search

basic configuration

git config --system parameter value # update system wide config, ie. /etc/gitconfig
git config --global parameter value # update user config, ie. ~/.gitconfig
git config --local parameter value  # update local config of the repository under the current directory, ie. .git/config
git config --global user.name "John Doe"
git config --global user.email "john.doe@mail.com"
git config --global core.editor vim
git config --global alias.co checkout # git co will be an alias for git checkout
git config --list

Make git slog be an alias for git log --oneline. In the file ~/.gitconfig, add:

[alias]
  slog = ! "git log --oneline"

More complex alias:

[alias]
  alias_name = ! "git cmd1; git cmd2; git cmd3; ...;"

create new repository

cd <project_directory>
git init # will create .git directory
git add .
git commit -m "first commit message"

create a bare repository from a working copy

git clone --bare <project_directory> <project_bare_directory>
e.g: git clone --bare myproject myproject.git

add changes to the index (staging area)

git add <filename>
git add -p # interactive mode: will request which changes to add, chunk by chunk
git add . # add all files, even the ones not under version control yet

discard changes added to the index

git reset # discard all changes from the index; the changes are still available in the working directory
git reset <filename>
git reset -p # interactive mode: will request which changes to discard, chunk by chunk

commit the changes

git commit # create a commits based on the content of the index
git commit -a # add to the index all the modified files already under version control and create a commit
git commit -m "commit message"

correct errors in the last commit

# modify the required files or 
# do not modify anything when the correction applies to the commit message only
git add <filename> # skip this step when the correction applies to the commit message only
git commit --amend

undo several commits

git reset <commit_id> # options are available (see: git help reset): --hard, --soft, ...

create new branch

git branch <branch_name> # create a branch with tip pointing to HEAD of the current branch
                         # and do not checkout to the new branch
git checkout -b <branch_name> # same as above but checkout to the new branch
git branch <branch_name> <commit_ID> # create branch with tip pointing to <commit_ID>

checkout a branch, a given commit

git checkout <branch_name>
git checkout <commit_ID> # here we are not on any branch anymore (see output of git branch)

list branches

git branch # local
git branch --all # include remote tracking branches

comparison between commits, index and working directory

git diff          # show diff between index and working directory
git diff --staged # show diff between HEAD and index
git diff --cached # same as: git diff --staged

git diff commit_ID1 commit_ID2 # show diff between commits
git diff commit_ID             # show diff between commit_ID and working directory
git diff HEAD                  # show diff between HEAD and working directory
git diff --staged commit_ID    # show diff between commit_ID and index

explore the commit history

git log
git log --oneline # short version
git log --graph # commit graph
git reflog # path of all commit we traversed across, independent of branch change, reset, etc

find which commit added a given string inside the source code

git log -S "<string_to_search>" --all --source

create remote branch from local branch

git push <remote_name> <local_branch_name>:<remote_branch_name>
eg: git push origin issue10:patchissue10

remove remote branch

git push <remote_name>  :<remote_branch_name>
eg: git push origin  :patchissue10