Friday 22 July 2011

Git

A summary of things I need to do with Git and can never quite remember how:

Checkout a URL from Github:
git clone https://github.com/mattiaslinnap/citymapper-web
 
To pull a branch from someone else's github repo in to your local one:
 git pull [copy link from github] [branch name]
 git pull  git@github.com:adamcstephens/dust.git  their-branch-name

I made changes locally now I want to push them into a separate branch and then on to github

git checkout -b many_changes
git push origin many_changes


Everything has gone wrong. REVERT!

git reset --hard HEAD

Revert to remote head:

git reset --hard origin/master


Oops! - I commited to wrong branch - undo commits but keep the changes on my local area. more info

git reset --soft HEAD^

Master has gone to hell. My current branch should be master instead

git merge --strategy=ours master
git checkout master
git merge GOOD_BRANCH

Everything has gone wrong. AND I have a local commit to revert

git reset --hard ORIG_HEAD

Revert to last committed state:

git reset --hard


Revert to last committed state from TIME ago:

git reset --hard master@{"10 minutes ago"}   

Review unpushed commits and squash:

git rebase -i

Reverting local changes:

git checkout -- file

Go back in Time:

git log -3
git checkout TAG

OR:
git checkout master^ (go back 1 commit from master)
git checkout HEAD^^ (go back 2 commits from where i am now)
git checkout HEAD~3 (go back 3 commits from where i am now)


Search:

Search thru git logs on my commits only:

git log --all-match --grep=search_string --author=andy

Git logs with follow:

git log --follow

Search for component:

git log -p -S SEARCH_FOR optional_limit_by_dir


Complex multi revert pattern:
 git checkout -b temp
 git rebase -i

  # If we get a merge error then you need to remove more commits as some more modern ones depend on the ones removed
  git checkout master
  git checkout temp -- .   #checkout  branch into the directory '.'
  git commit -a


Some commit has introduced a bug somewhere - lets go find it:

Bisect

Pull code then review before release

git fetch
summary:
  git log HEAD..origin
giant diff:
  git log -p HEAD...origin
git merge origin


Standard GIT workflow:(create a myfeaturebranch from the master branch)

git checkout -b myfeature master

while(I have code):
    (code)
    
git pull --rebase origin master

final merge:

git checkout master
git merge myfeature  OR git merge --no-ff myfeature OR git merge --squash myfeature
git branch -d myfeature
git push origin master


NOTES: 
--no-ff = No Fast forward = Keep the fact I worked on a separate branch.
(default) = Fast Forward = Fast forward all changes in to the master. 
General Rule: Use --no-ff if you worked on something big and want the branch history.
--squash = Squash this local branch's commits into 1. Makes git logs easier to read
git pull --rebase origin master = remove local changes. Pull master. Re-apply your changes


REBASE ADVICE:
If you can rebase OFTEN do it. If there are many differences between master and your branch do a merge instead. Rebase is only for LOCAL branches not pushed ones.

REBASE WARN:
Do not rebase a public 'pushed' branch that someone else is using. EG: If you fork a github project your fork is publicly available so do not rebase this fork with the original master. Instead use git fetch, git merge


Delete remote branch
git push origin --delete 

Merging:
git mergetool

Funky review & merge tool:
gitk

Using my file or there file on conflict:

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"


Oops! I commited to the wrong branch

How to rebase - This is like merging but better. It updates the origin (head) then applies each of your commits so you have no 'merges'.


More Git Help

I'm in git Merge Hell!

Great Git learning web game

Tool to simplify your git branching Git Flow

4 comments:

  1. reset to remote master:
    git reset --hard origin/master

    ReplyDelete
  2. If you want to rebase from branch to master but you get lots of conflicts. Squash your commits into 1 and then cherry-pick from master:

    git checkout branch_name
    git rebase -i HEAD~N [N= num of commits]
    git checkout master
    git cherry-pick

    ReplyDelete
  3. To track your new github fork locally:
    git remote add up git@github.com:bootandy/thing_on_github.git
    git push -u up master

    ReplyDelete
  4. If cmdline starts asking for a password:
    $ git push origin ab-rm-windows Password for 'https://bootandy@github.com':

    https://mycyberuniverse.com/en-gb/how-fix-fatal-authentication-failed-for-https-github-com.html

    ReplyDelete