In this post I’ll share a really small but handy tip I discovered while using Git.
Let’s say you make some commits on your local repository which you later find undesirable. You can of course use git reset –hard to put the repository in previous state that doesn’t have those commits.
Let’s assume that later (perhaps after adding a few fresh commits) you try to push this to the remote repository, git will refuse by default because the remote repository has more commits.
To force git to accept the push you need to add the -f flag.
git push -f
Note: you should only force a push if you are the only one with access to the repository or you are sure that other users haven’t cloned the existing state of the remote repository, otherwise forcing a push could lead to problems of diverging history for other users.
By default, when running Git command on repositories accessed via SSH, Git uses your default private key, i.e. /.ssh/id_rsa. Suppose you have multiple SSH keys and you want Git to use a specific non-default one to perform a command, you may do this using a command like
GIT_SSH_COMMAND="ssh -i ~/.ssh/your_rsa_key" git push origin master
Note that you need Git 2.3 and newer.
How to tell git which private key to use? http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use.
Suppose you are working on a Python project and want to quickly install a public package from Github, you can do this using a command like:
pip install -e git://github.com/someusername/package-name.git#egg=some-packagename
If the repository is private and your SSH private key has been added to the repository’s access list, then you can access it using a command like:
pip install -e git+ssh://email@example.com/someusername/package-name.git#egg=some-packagename
When setting up automatic deployment on a software project using git, you may want your server to be able to automatically pull changes from the repository. Popular repo hosts like Github and Bitbucket will usually need your server’s public SSH key to allow access. Assuming you have a server with multiple SSH keys, it may become important for you to specify which SSH key to use for specific users when using git. Here I explain how to make git use a specific private key when accessing Github.
IdentityFile <path to id rsa file>
Now you can do
git clone firstname.lastname@example.org:username/repo.git.
Make sure the permissions on IdentityFile are 400.
ssh – How to tell git which private key to use? – Super User. http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use
If on Ubuntu, open /etc/gitweb.conf and make sure that
$projectroot = "<path to repositories folder>"
One way would be to re-initialize the remote repository completely cleanly and then do a push from local.
On the server, in the directory where the bare repository should live, do
git init --bare
On the client (local repo), set the new origin if it has changed, and push to the server, and set up tracking again.
git remote set-url origin <Remote_URL>
git push origin master
git branch --set-upstream-to=origin/master master
Sometimes you may be working on your Git repo and discover that you have made changes in several files and need to stash changes to only some of those modified files. How do you accomplish that? Well, there’s a handy command that goes as follows:
git stash -p
This will go through each modified file and give you an option to stash the hunk by entering ‘y’, not stash the hunk by entering ‘n’, or perform other actions. A full list, found on Stack Overflow is shown below:
y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
a - stash this hunk and all later hunks in the file
d - do not stash this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help