Git: Some examples

Hi,

my git example collection…to be continued
Global Config
Set your username

git config --global user.name "Michael"

Your E-Mail Address

git config --global user.email info@michlstechblog.info

Your favorite Editor on Windows

git config --global core.editor "'D:/Tools/Notepad++/notepad++.exe -nosession -multiInst'"

and on UNIX

git config --global core.editor vi

Disable automatic Linefeed conversation

git config --global core.autocrlf false

Convert lineending while cloning

git config --global core.eol lf
git config --global core.autocrlf input

Set the default UI encoding

git config --global gui.encoding utf-8

View config

git config
git config --list
git config user.name

Mergetool

git config --global merge.tool xxdiff
git config --global merge.tool kdiff3
git config --global merge.tool meld

Mergetool on Windows

git config --global merge.tool winmerge
git config --global winmerge.cmd = "'C:/Program Files (x86)/WinMerge/WinMergeU.exe'" -e "$LOCAL" "$REMOTE"

Configure a proxy server

git config --global http.proxy http://192.168.233.13:8080
git config --global https.proxy http://192.168.233.13:8080

Colored UI

git config --global color.ui true

Aliases

git config --global alias.co checkout
git config --global alias.unstage 'reset HEAD --'

To get some debug information. Set the following environment variables

GIT_CURL_VERBOSE=1
GIT_TRACE=1
GIT_TRACE_PACKET=1

Note: HEAD => Pointer to your current branch/commit. The branch points to the last commit

Project based
Creates a new git repository in the current working folder

git init

Clone an existing project

git clone https://github.com/knxd/knxd

Just clone a repository (not checkout a working copy, just the latest commit) and get one file as working copy

git clone -n git://path/repository.git --depth 1 clonerepo
cd clonerepo
git checkout HEAD FileToGet

Pushing local changes to origin in branch master

git push origin master

Pushing all branches

git push origin --all

Push local branch v1.0.0.0 as remote branch v1.0.0.0

git push origin v1.0.0.0:v1.0.0.0

Refresh remote branch and create a new branch

git fetch origin
git checkout -b newBranchFromMaster origin/master

Checkout a pull request. For example Pull with ID 666. Fetch the pull request into a new branch

git fetch origin pull/666/head:myPullrequestBranch
git checkout myPullrequestBranch

Some error message that often occurs while pushing your changes up to github:
By using git protokoll

fatal: remote error:
You can't push to git://github.com/user_name/project.git

or by https protocol

remote: Permission to user_name/project denied to GitUser.
fatal: unable to access 'https://github.com/user_name/project/': The requested URL returned error: 403

Pushing works only over ssh. git@github.com/user_name/project. The Username must be set to git otherwise you get an Permission denied (publickey) error.

Remote Repositories
To which remote server is the project configured

git remote -v
origin https://github.com/Race666/install-knxd.git

Add an additional remote repository

git remote add fork https://github.com/Race667/install-knxd-fork.git

Set/overwrite the url, maybe change from git:// to https://

git remote get-url origin
git://github.com/user_name/project
git remote set-url origin git@github.com/user_name/project

get remote repository to fork/master and other branches fork/branch1 …. No merge is done at this time

git fetch fork

Show remote repository

git remote show origin
git remote show fork

Rename the my local remote repository name

git remote rename fork forkNewName

Remove the remote repository

git remote rm forkNewName


git pull

Covert a repository in a bare repository. A bare repository does not held a working copy of your project

git config --bool core.bare true

Refresh your working copy from repository

git reset --hard

Clean up your working copy

git clean -d -x -f

Add all files to a respositories staging area (INDEX)

git add --all

Add an specific file to staging

git add test/test.txt

Undo a add changes in staging area

git reset HEAD test/test.txt

Also reset the file to the latest commited version

git checkout -- test/test.txt

Unstage all

git reset HEAD

And reset working directory

git checkout -- *

Show difference between repositories staging area and working tree(uncommited changes)

Remove a file from staging (INDEX), the file would also deleted from working directory

git rm test/test.txt

Just remove a file from staging area but leave the file in the working directory. Maybe you have forgotten to add the file to .gitignore

git rm test/test.txt --cached

Rename a file

git rm test/test.txt test/testmoved.txt

git diff

Show differences from INDEX which are to commit

git diff --cached
# or
git diff --staged

Commit changes

git commit -m "Changes done. v1.4.0.0"

Stage and Commit changes (git add && git commit)

git commit -am "Changes done. v1.4.0.0"

Commit from outside the working directory

git --git-dir=/home/michael/docs/.git/ --work-tree=/home/michael/docs/ commit -am "Changes commited"

Changes the last commit, may be you forgotten to add a file or want to edit the commit message

git commit --amend

Same as above but read commit message from a file

git commit --amend -F ./mynew_commit_message.txt

Delete last commit(s) (its not really a delete, it’s just set the head to a specific commit), determine commit to set

git log

and set HEAD to the commit. –soft leaves the working tree untouched so you can stage and commit your changes again.

git reset --soft e6ee3dba38b0b771668dfbc229c4295428566c63

If you want to push such a change, you must force the push(i.e. if you want to delete at github)

git push origin +master

List commits

git log --pretty=oneline

Show log from outside the working directory

git --git-dir=/home/michael/docs/.git/ --work-tree=/home/michael/docs/ log

List last 3 commits and changes/patch -p

git log -3 -p

Word diff , makes no sense for source code

git log -3 -p --word-diff

Formated log output examples, –graph shows a simple branch/merge history

git log -3 --pretty=[online|short|full|fuller]
git log -3 --pretty=format:"%h - %an, %ar : %s"
git log -3 --pretty=format:"%h - %an, %ar : %s" --graph
# Newer then
git log -3 --since "2017-05-22"
git log -3 --after "2017-05-22"
# Older then
git log -3 --until"2017-05-22"
git log -3 --before "2017-05-22"
# From a committer
git log --committer Michael
# From a Author
git log --author Michael

 
Get the short id of the last commit

git rev-parse --short HEAD

Go to/undo last commit and loose all changes, delete last commit

git reset --hard HEAD~1

or get commit id and reset working directory to this

To push the commit deletion to github you have to force pull with a + sign in front of your branch name

git push origin +master

git log
git reset --hard 7f1393782c1daeb008378e10090aa9a51d64082d

Go to a specific commit

git reset --hard f9a0864228b7c93e2974907c6439761f3d5acda7

List files in index. Its the virtual state of an repository, and does not match the files in the working copy

git ls-files --stage

Finding a file in a repository

git ls-files --stage | grep -i Filename

Create a tag

git tag -a v1.4.0.0 -m v1.4.0.0

If you are owner of a gpg key. Sign a tag

git tag -s v1.4.0.0 -m v1.4.0.0

Check signing of a tag

git tag -v v1.4.0.0

Tag a commit

git tag -a v1.0.0.0 -m v1.0.0.0 f9a08648

Show the commit for a specific tag

git rev-list -n 1 v1.0.0.0

Publish a tag, not done by default

git push origin v1.0.0.0

Or publish all tags

git push origin --tags

Delete a tag

git tag -d v0.7.7

And push the delete tag (i.e. to github)

git push origin +master :v0.7.7

Checkout a version from a specific date
# ToTest: git checkout ‘master@{2009-07-27 13:37:00}’

git checkout `git rev-list -n 1 --before="2009-07-27 13:37" master`

Checkout a tag

git checkout tags/v0.12.12

List details of a tag

git show --name-only v4.11

Checkout latest

git checkout master

Get last commits from origin

git pull

List all branches

git branch -a

List branch with its last commit

git branch -v

Create a branch

git branch MyDevBranch21

Checkout a branch

git checkout MyDevBranch21

Create branch from commit

git branch newbranch 3af8c781d89a34a76f7435648676744673d78e0c5c230

Delete a branch on github

git push -d origin merge

Merge the branch

git checkout master
git merge MyDevBranch21

List merged branches

git branch --merged

List branches not merged

git branch --no-merged

On Merge conflicts edit the unmerged file, edit the marked section. <<<<< => current branch, ===== => spliter, >>>>>> end branch to merge

<<<<<<< HEAD include <stdio.c> ======= include <stdio.h> >>>>>>> mydevbranch

and add the changed file to stage.
or start mergetool

git mergetool

Stash dirty (not commited) changes of your working directory (by default untracked and ignored files are not stashed)

git stash

Reapply stashed changes

git stash pop

Reapply stashed changes and keep stash

git stash apply

List all stashes

git stash list

Apply stash from list

git stash pop stash@{2}

If you have a detached HEAD, this occures when you checkout a commit, then you makes changes in this commit and went back to master the changes seems lost. To get it back see the ref log for your lost commit

git reflog --all
ec51ec1 HEAD@{0}: checkout: moving from 88d682c130e8556fa80f4a42a428ee66070751a2 to master
88d682c HEAD@{1}: commit: midcommit2

or

git log -g
--pretty=oneline
ec51ec1050c381c90fb5b82dccebb27772444d01 HEAD@{0}: checkout: moving from 88d682c130e8556fa80f4a42a428ee66070751a2 to master
88d682c130e8556fa80f4a42a428ee66070751a2 HEAD@{1}: commit: midcommit2

and merge the detached commit/head “commit: midcommit2” => HEAD@{1}

git merge HEAD@{1}

or with the hash, this makes it unique because HEAD@{x} changes each time you make a checkout,reset, merge or commit…

git merge 88d682c130e8556fa80f4a42a428ee66070751a2

Change E-Mail and Author of a commit => You have to rebase the commit

git rebase -i midcommit2

Change the date of an old commit. Rebase the commit interactive. This oens a text editor

git rebase -i 0224b7acfa9cafe30251efc13ec59244d2f1b488^

Replace pick with edit and save the changes

edit 0224b7a Some commit description

Change the date

GIT_COMMITTER_DATE="Thu Sep 25 23:55:32 2019 +0200" git commit --amend --no-edit --date "Thu Sep 25 23:55:32 2019 +0200"

and save it

git rebase --continue

Your text editor opens and you have to set all commits to change to edit

e b136cc3 Clone commit
e a38ac0e Last commit
e d0a1dc8 detached
# Rebase f737209..

After saving and closing the editor, take changes and go to next commit(s)

git commit --amend --author "The Newname <email@newdomain.org>" --no-edit
git rebase --continue

Exclusions
Create a .gitignore file in the repository root. # are comments. Example Filepatterns:

*.exe*
*.log
*.rl
log.txt
old*/**
*.opt-????????*
*.lic-????????*

Add just specific file type to a git repository. Create Create a .gitignore file in the repository root

# ignore everything
*
# Add directories
!*/
# And add file types you want
!*.one
!*.two
!*.etc

List all commits of a file

git log --follow yourFilename

List details of a commit

git show 8af8c781d89a34a76f70049b7734673d78e0c5c230

and with changed files

git show --summary 8af8c781d89a34a76f70049b7734673d78e0c5c230

Find when a file was added

git log --diff-filter=A -- yourPath/yourFilename

Difference between 2 commits

git diff 3af8c781d89a34a76f7435648676744673d78e0c5c230 8af8c781d89a34a76f70049b7734673d78e0c5c230

Sync your forked project from the master you forked from

# Clone my repository
git clone https://github.com/myGitHubUser/knxd.git knxd
cd knxd
# The repository you forked from
git remote add upstream https://github.com/knxd/knxd.git
git fetch upstream
git pull
git push origin +master

If a merge conflict occures (error: Merging is not possible because you have unmerged files…fatal: Exiting because of an unresolved conflict. )abort merging before git pull

git merge --abort
git pull
....

Optimize a repository and delete unneeded files

git gc --auto

Run Health check and check objects in database

git fsck

Michael

Advertisment to support michlstechblog.info

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.