Thursday, May 21, 2015

Git and Github


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
git cherry-pick [commit-hash]

pull request for a single commit

check commit not pushed (ahead)

delete a git commit

    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]

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:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
2. Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:
git push origin -u new-name

As mention in the comment there are several strategies in the documentation. You can also find them here: 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

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.

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

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


  1. Undo local commits, keep the changes,

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