Thursday, May 21, 2015

Git and Github

Git

https://services.github.com/kit/downloads/github-git-cheat-sheet.pdf


https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository


If you want to revert changes made to your working copy, do this:
git checkout .
If you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:
git reset
If you want to revert a change that you have committed, do this:
git revert ...
If you want to remove untracked files (e.g., new files, generated files):
git clean -f 
Or untracked directories:
git clean -d

Use ~/.git-credentials username and password


git config credential.helper store

git cherry-pick

https://stackoverflow.com/questions/2369426/how-to-move-certain-commits-to-another-branch-in-git
git cherry-pick [commit-hash]

pull request for a single commit
https://stackoverflow.com/questions/25955822/git-cherry-pick-a-single-commit-for-pull-request


check commit not pushed (ahead)
https://stackoverflow.com/questions/4082175/how-can-i-check-which-commits-have-not-been-pushed-to-origin

https://stackoverflow.com/questions/3882583/how-to-discard-local-commits-in-git



delete a git commit

https://ncona.com/2011/07/how-to-delete-a-commit-in-git-local-and-remote/

    show commits

git log --pretty=oneline --abbrev-commit
    delete in local repo
    git reset --hard HEAD~1     <-- undo latest commit


  git reset --hard [commit_hash]   <--- reset to this commit

    delete in remote repo
    git push origin +[branch_name]




https://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

git fetch -p






Rename a local and remote branch in git


If you have named a branch incorrectly AND pushed this to the remote repository follow these steps before any other developers get a chance to jump on you and give you shit for not correctly following naming conventions.
1. Rename your local branch.
If you are on the branch you want to rename:
1
git branch -m new-name
If you are on a different branch:
1
git branch -m old-name new-name
2. Delete the old-name remote branch and push the new-name local branch.
1
git push origin :old-name new-name
3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:
1
git push origin -u new-name
https://stackoverflow.com/questions/17704119/how-to-prefer-files-from-one-branch-during-a-merge


As mention in the comment there are several strategies in the documentation. You can also find them here: http://git-scm.com/docs/git-mergeYou are looking for either git merge -s recursive -X ours or git merge -s recursive -X theirs depending on the branch that you are on. Be careful with these as you can accidentally miss changes to your files from the other branch and these would be overwritten.
Another method, which I prefer due to more control, is to git checkout <other-branch> -- <list of files>. This way I don't accidentally overwrite a file with the wrong changes.

Just to be sure I understand well, in my case if I do git merge -s recursive -X ours new_feature from master I will get the changes from new_feature but the conflicting files will stay as they are on master


https://stackoverflow.com/questions/6000919/how-to-check-the-differences-between-local-and-github-before-the-pull

git pull is really equivalent to running git fetch and then git merge. The git fetchupdates your so-called "remote-tracking branches" - typically these are ones that look like origin/mastergithub/experiment, etc. that you see with git branch -r. These are like a cache of the state of branches in the remote repository that are updated when you do git fetch(or a successful git push).
So, suppose you've got a remote called origin that refers to your GitHub repository, you would do:
git fetch origin
... and then do:
git diff master origin/master
... in order to see the difference between your master, and the one on GitHub. If you're happy with those differences, you can merge them in with git merge origin/master, assuming master is your current branch.
Personally, I think that doing git fetch and git merge separately is generally a good idea.


https://stackoverflow.com/questions/3380805/checkout-old-commit-and-make-it-a-new-commit

git rm -r .
git checkout HEAD~3 .     (or commit hash)
git commit
After the commit, files in the new HEAD will be the same as they were in the revision HEAD~3


https://stackoverflow.com/questions/12055198/find-out-git-branch-creator/19135644

List Remote Git Branches By Author sorted by committerdate:
git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n

Revert merge commit

https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch

5 comments:

  1. Undo local commits, keep the changes, https://stackoverflow.com/questions/4850717/how-to-cancel-a-local-git-commit


    Use --soft instead of --hard flag: git reset --soft HEAD^

    ReplyDelete
  2. https://www.atlassian.com/git/tutorials/using-branches/git-merge

    ReplyDelete
  3. https://stackoverflow.com/questions/15232000/git-ignore-files-during-merge

    ReplyDelete
  4. https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting

    ReplyDelete
  5. https://railsware.com/blog/2014/08/11/git-housekeeping-tutorial-clean-up-outdated-branches-in-local-and-remote-repositories/

    ReplyDelete