A bunch of useful commands and tips for using git.
It is recommended to use git via ssh instead of https. To save yourself the trouble of entering your password everytime, copy your ssh public key into your git account. The public key is in
$HOME/.ssh/id_rsa.pub file; if you dont have it, then generate it by typing this in terminal
and press enter to all the questions. Now log in to your git account in your browser, go to settings page where you will find a place to copy the ssh key.
When you are collaborating with me on some project, we have to share code via git. When I update the code in my repository, you must get a notification that there is an update. For this to happen, you must watch the repository. Go to the webpage of the repository and look for "Watch" link and enable it.
To add all modified files which are already tracked
git add -u
Suppose you have not pushed your commit to remote. Then undo last commit while keeping changes from it (unstaged)
git reset HEAD^
An equivalent command is
git reset --soft HEAD~1
If you want to throw away the changes
git reset --hard HEAD~1
To remove all untracked files in the current directory
git clean . -f -x
To remove all untracked files and directories in the current directory
git clean . -f -x -d
If you want to use an old version of the code, then find the SHA checksum of the commit using
git log and then do
git checkout <checksum>
The HEAD is now set to the selected commit and you can see this by doing
git branch. If you now want to go back to master, do
git checkout master
Create a new branch
git branch my_branch
Make your changes, add files and commit them to your branch. Then push them to remote
git push origin my_branch
To merge your branch into master
git checkout master git merge my_branch
You can now push master to your origin and then create a pull request.
git checkout master git remote add upstream email@example.com:upstreamname/projectname.git
Now you can get updates from upstream
git fetch upstream git merge upstream/master
To send these changes to your own remote
git push origin master
git checkout master git pull origin master git pull upstream master git checkout my_branch git rebase master
The last step might indicate some conflicts which have to be resolved.
You may want to squash several commits into one before creating a pull request.
git rebase -i yourbranchname
git push -f origin yourbranchname
git branch -D my_branch git push origin :my_branch
git checkout master git remote add contributor git://github.com/contributor/projectname git fetch contributor git merge contributor/newfeature git push origin master