Git

From Torben's Wiki
Jump to navigationJump to search


Git Basics

Basics

clone a repository

git clone git@github.com:entorb/rememberthemilk.git

perform a commit

git add .
git commit -m "my commit message"
git push

Git Settings

!!! End of Line !!!

Force Git for Windows to use Linux end of line: LF (\n) instead of CRLF (\r\n)

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

colorful output

git config color.ui true

log: one line per commit

git config format.pretty oneline

Branching

# create new branch
git checkout -b history-change
# del branch
git checkout master
git branch -d history-change

Remove branch not (any more) on master

prunes tracking branches not on the remote. from https://stackoverflow.com/posts/28464339/timeline

git remote prune origin 

list branches that have been merged into the current branch.

git branch --merged

gitignore

Re-apply .gitignore

see https://github.com/entorb/tools/blob/master/git-tools/git-gitignore-reapply.cmd


Reverting

reset to a commit

get hash of commit
git log --oneline
git reset --hard HEAD~1
# git push -f

revert a commit

get hash of commit
git log --oneline
git revert <commit hash>
# git push -f

Reset branch to remote branch

from [1] Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master
# git push -f

Squashing

Squash last 3 commits

git reset --soft HEAD~3 &&
git commit

revert local git to remote repository

from [2]

git reset --hard HEAD
git clean -f -d
git pull

GIT Commit to an existing Tag

from https://gist.github.com/danielestevez/2044589

1) Create a branch with the tag
	git branch {tagname}-branch {tagname}
	git checkout {tagname}-branch

2) Include the fix manually if it's just a change .... 
	git add .
	git ci -m "Fix included"
    or cherry-pick the commit, whatever is easier
	git cherry-pick  {num_commit}
	
3) Delete and recreate the tag locally
	git tag -d {tagname}
	git tag {tagname}

4) Delete and recreate the tag remotely
	git push origin :{tagname} // deletes original remote tag
	git push origin {tagname} // creates new remote tag
		
5)  Update local repository with the updated tag (suggestion by @wyattis)
	git fetch --tags

This is based on https://gist.github.com/739288 thanks to nickfloyd for it

Hacks

Chmod

set executable flag. see [3]

git update-index --chmod=+x file

delete complete commits history

First Method from [4]

# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH
# Add all the files:
git add -A
# Commit the changes:
git commit -am "Initial commit"
# Delete the old branch:
git branch -D master
# Rename the temporary branch to master:
git branch -m master
# Finally, force update to our repository:
git push -f origin master

Second Method via "deleting .git folder" also from [5]

# Clone the project, e.g. `myproject` is my project repository:
git clone https://github/heiswayi/myproject.git
# Since all of the commits history are in the `.git` folder, we have to remove it:
cd myproject
# And delete the `.git` folder:
rm -rf .git
# Now, re-initialize the repository:
git init
git remote add origin https://github.com/heiswayi/myproject.git
git remote -v
# Add all the files and commit the changes:
git add --all
git commit -am "Initial commit"
# Force push update to the master branch of our project repository:
git push -f origin master

Alternative method:
https://www.willandskill.se/en/deleting-your-git-commit-history-without-removing-repo-on-github-bitbucket/

Error handling

Error:

git pull --tags origin master
[rejected] ... (would clobber existing tag)

Fixed by updating local tags with remote tags

git fetch --tags -f

Tag handling

Delete a tag locally

git tag -d myTag

Delete a tag remotely

git push --delete origin myTag

History

Checking History

Display all commits that modified a certain file

git log --no-decorate --pretty=format:"%h%x09%ad%x09%an%x09%s" --date=iso -- data/de-districts/de-district_timeseries-02000.tsv
# %x09 : tab

Modifiying History

Merge 2 repos

see https://github.com/entorb/tools/blob/master/git-tools/git-merge-2-repos.cmd

delete a dir from historiy using bfg

using bfg

https://github.com/entorb/tools/blob/master/git-tools/git-bfg-cleanup-delete-history-of-dir.cmd

GitHub.com Tipps

create PR for another fork

  • on github.com propose an edit for 1 file
  • this creates a branch in our repo based on the fork, called patch-1 or similar
  • rename the branch at `github.com/<your name>/<your repo>/branches/yours`
  • now you can locally pull from your master to fetch the new fork and locally add more modifications
  • I suggest keeping "unmodified" base-branches of other forks, so easily create new PRs and branches based on them
  • to update the fork from he fork's master, use github.com

Notes of 2020

very nice tutorial: git - Der einfache Einstieg

Old notes

[6] [7]

git init # create an empty git repository in the current folder
# set some settings
git config --global user.name "Torben Menke"
git config --global user.email "torben.menke@XXX.de"
git config --global color.ui auto
git config -l # shows configuration

git add somefile.txt # one file
git add somefolder # one folder
git add . # all

git commit 
git commit -a -m "message" # commit all, message="message"

git pull ssh://tmenke@[IP]/[PATH] master
# edit
# commit
git push ssh://tmenke@[IP]/[PATH] master

git log
git log --pretty=oneline --abbrev-commit