====== Git Tips and Tricks ====== ===== Shortcut URLS ===== If you clone a lot of Xfce repositories, you can set a shortcut so you don't have to type out the full URL all the time. Simply run: git config --global 'url.ssh://git.xfce.org/git/.insteadOf' xfce: After this, you'll be able to do, for example: git clone xfce:xfce/xfdesktop and Git will automatically rewrite it to the correct URL. You can of course do this with git: and http: URLs as well. ===== Different Local Username? ===== If your ssh username is not the same as your local username, you can make an entry in ''$HOME/.ssh/config'' (create it if it doesn't exist) so you don't have to type it each time: Host git.xfce.org User $USERNAME Of course, replace ''$USERNAME'' with your actual username. ===== Branch Auto-Pushing ===== If you have a relatively new version of Git on your machine, you may get this message after running ''git push'': warning: You did not specify any refspecs to push, and the current remote warning: has not configured any push refspecs. The default action in this warning: case is to push all matching refspecs, that is, all branches warning: that exist both locally and remotely will be updated. This may warning: not necessarily be what you want to happen. warning: warning: You can specify what action you want to take in this case, and warning: avoid seeing this message again, by configuring 'push.default' to: warning: 'nothing' : Do not push anything warning: 'matching' : Push all matching branches (default) warning: 'tracking' : Push the current branch to whatever it is tracking warning: 'current' : Push the current branch This warning is harmless, but you might want to change the value both to make your pushes "safer" and to get rid of the warning. Personally, I set mine to "tracking": git config --global push.default tracking My rationale here is: - Using 'nothing' is annoying: you have to specify the full name to push each time (e.g. ''git push origin master''). - Using 'current' could result in you accidentally pushing a new branch (that is, one that doesn't exist on the server) by accident. - Using 'matching' will push **all** local branches that exist on the server. If you had some commits in another local tracking branch that you forgot about but didn't want to push yet, these will get pushed anyway. - Using 'tracking' will only push the current branch, and then only if the branch already exists on the server. Yes, this means if you want to push multiple branches you have to push several times, but how often do you need to do that? It also means that if you're pushing a new branch, you have to explicitly name the branch on the command line the first time you push it, but, again, how often do you do that? ===== Pushing to a New Repository ===== Git, annoyingly, requires you to know a few magic incantations when you push code to a brand-new remote repository. We'll assume that you've already created a local repository using ''git init'', have added files with ''git add'', and have made some commits with ''git commit''. After that, you need to add a remote to point to the repository on the server. By convention, the default remote is called ''origin'': git remote add origin $REPOSITORY_URL Of course, replace ''$REPOSITORY_URL'' with the URL of the repo you want to push to. If the remote repository is completely empty and has no commits in it, you have to explicitly tell git what source branch you want to push (''master'') and where it should go on the remote (''refs/heads/master''): git push origin master:refs/heads/master From now on, all you'll have to do is ''git push origin''. If you want to make it even easier, you can set up some configuration to automatically decide where to push: git config branch.master.remote origin git config branch.master.merge refs/heads/master From now on, you can just do ''git push'' and it will know what to do. When you clone a remote repository, these two configuration entries are set up for you automatically, but since you started with a self-created local repository, this couldn't happen.