Created: 2017 May 19th
Updated: 2021 May 30th
HEAD^
means one revision back (this is the local step) then you
force push to origin. without the force flag, you’ll get an error from
git - telling you about the one incoming change from remote, which
you’re trying to remove:)
src
How to apply a git patch file.
Good resource here: https://www.devroom.io/2009/10/26/how-to-create-and-apply-a-patch-with-git/
If you haven’t done it completely, i.e., in gitbash you see something
like: Username@Host MINGW64 /d/code/your-project (feature|REVERTING)
then you can use this to abort
Don’t want to have the default branch called master?
Thanks to Mathias Bynens for this one.
Create a new repo and push it to GitHub.
The first four commands can be ignored if you have a repo you’re already working on (git)committing to.
Assuming that you are working on the master branch then
You can set it to whatever branch you want to track changes for
This will mean you can just do git pull
and the latest changes will
be pulled to your origin
Want to make your feature branch and get it on GitHub?
Make your branch first then:
Fork other users repo in GitHub, then clone to your machine.
Add the remote repo:
Create your branch:
Check it out:
If adding a folder use.
Make your commit and push to your new branch.
Manage the rest of the PR via GitHub
First configure the local to point to the remote upstream
You then use git merge
to update any branch on the upstream
repository:
Take a look at syncing a fork for more details.
switching the base
. Now GitHub will compare your
fork with the original, and you should see all the latest changes.Using two factor authentication? Then use the following so you’re not
adding in your auth token each time you want to push
your code.
origin
urlIf you want to change the origin url you can use the set-url
command
Via terminal navigate to your code folder.
Add your files.
Adding a folder use the following syntax or it’ll get added as a BLOB.
Commit to local repo.
To add your files to the remote repo, first add your remote repo
For more info check out: adding an existing project to github using the command line
Delete local branch.
Remove local branches that are not on the remote
.
Remove local branches that were created from remote branches.
How to merge the master branch into the feature branch? This will come up often if you are working on a team with other devs and you want to update your feature branch to include the latest changes.
If you want to merge project-a into project-b:
If you have .env
files that are tracked by Git and want to ignore
them so your API keys don’t get added to GitHub use:
First add the folder to your .gitignore
then remove the folder from
your local git tracking with:
So you make a clone, make some changes then realise that you need to add it to your GitHub account before making a pull
You just need to set the origin
to yours then add the upstream
as
the original origin
make sense?
So change origin
to yours:
Then add upsrtream
as theirs:
Now it should look something like this:
upstream
repositoryIf you no longer need a reference to a forked repository then remove it with the following:
If you are pushing right after a commit, you can use
git push --no-verify
to avoid running all the tests again.
If you make a trivial change and want to commit
git commit -m 'some detailed message' --no-verify
will skip your
precommit
and prepush
scripts.
Read this for more detail on how to revert.
This was the simplest approach I found:
Rather than use the last part I unstaged the changes in VSCode which I think did the same thing.
.gitconfig
detailsThere are three levels for Git config:
System level
Global level
Repository level
View All Settings
If you are having issues with changes showing in Windows Git and not Windows Subsystem Linux Git (For a Windows WSL Dev set-up) then check the settings of each environment by using:
Remove any conflicting settings then try again.
If you want to rename the current branch, you can do:
A way to remember this, is -m
is for “move” (or mv
), which is how
you rename files.
Want to know what work you have done on a repo? Use git reflog
to
displpay all the commits.
Get your SSH set up on your machine and add a key to GitHub, more on that here: https://egghead.io/lessons/javascript-how-to-authenticate-with-github-using-ssh
You will then need to pick your Clone with SSH option from the Clone or download section on your repo page.
Once you have taken the link from there you will need to set the repo remote to the SSH URL
Where username is the username
of the repo owner and
repo-name-here
is the name of that user’s repository.
Check that there are no rsa
files here before continuing, use (bash
or Git bash if you’re on Windows):
If there’s nothing there then generate a new keygen with:
If you decide to use a password for your SSH key see SSH Keys With Passwords
Now using ls -al ~/.ssh
will show our id_rsa.pub
file.
Add the SSH key to the SSH agent:
Add RSA key to SSH with:
Copy your key to clipboard with one of the following:
Add a new SSH Key to your GitHub profile from the settings page by clicking the New SSH key button and paste in your key. Save it…
Then authenticate with:
If you go back to the GitHub setting page and refresh the key icon should go from black to green. 🎉
If you add a password to your SSH key you will find yourself entering the password to authenticate on each [pull, push] operation. This can get tedious, especially if you have a long password in your keys.
Add the following line to your ~/.ssh/config/
file:
Open or create the ~/.ssh/config
file with:
The SSH agent will also need to be started on each terminal session
now to store the keys in, add the following to your ~/.bashrc
file:
Open the ~/.bashrc
file with:
Now the SSH agent will start on each terminal session and you will
only be prompted for the password on the first pull
, push
operation.
If you have more than one GitHub account or if you have AWS code
commit account then you will need to set up a config
file, add your
SSH key the same as detailed in
How to authenticate with GitHub using SSH
and give the key a different name:
You can delete all cached keys before, with:
You can check your saved keys, with:
Set up the SSH config file, check to see if you haven’t got a config
file already set up with:
If you haven’t got a config
file there then:
Use your text editor of choice, in this example we’ll use nano
:
Add your configuration:
Clone your repo and modify the config file of the repo as detailed here: Specify multiple users for myself in .gitconfig?
There’s a great Gist detailing this here for more detail if needed.
If you want to avoid creating multiple SSH keys for different
environments and move your .ssh
folder from one machine to another
then you can do the following:
Copy your .ssh
and .gitconfig
files:
Copy from Linux to Windows
Copy from Windows to Linux
Start the SSH agent with:
Add your SSH key to the ssh-agent
with:
Then authenticate with:
SSH can be tunnelled over HTTPS if the network you are on blocks the SSH port.
Test if SSH over HTTPS is possible with:
If you get a response then, edit your ~/.ssh/config
file and add
this section:
Check that you have a key already added with:
If nothing is listed then add in your key with:
Test that is has worked with:
Tired of typing your SSH key password because you made it a 32 characters and can’t stand the monotony anymore?
Still want to have a SSH key password on your existing SSH key?
Use:
Want to have different git credentials for one specific repository?
You can configure an individual git repo to use a specific user/email address which overrides the global configuration.
To list out the config for the repo:
From the root of the repo, run:
Whereas the default user / email is configured in your ~/.gitconfig
If you’re working on a team and there have been changes to the main branch you want to push your changes to, you can rebase before submitting a PR.
In this scenario we’re going to rebase our feature
branch off of the
develop
branch
Then use the prompts from there in conjunction with your text editor to add in the changes.
If you have a large file (like a package-lock.json
) that you want to
accept all the incoming changes from then.
Whilst you’re in rebase you’ll need to check out the file from your incoming branch then add it as the new file.
If you want to see the difference between two branches then use the git built in diff tool.
If you want to see the difference between two file across different branches then use.
Find the commit you want to revert to, then:
Then reset to the branch on the origin:
Reference: https://stackoverflow.com/questions/11829911/push-changes-without-pull
You can automate the creation of your projects gitignore
file using
the gitignore API.
Setup the API:
Add to your shell configuration:
Bash
checkout gi list
for the languages and editors supported. You can
issue the following command inside your project
If you find yourself using the same .gitignore
on your projects you
can create a global file (i.e. .gitignore_global
), and copy to your
new project.