Useful Git commands
- Useful Git commands
- Add a new repo from your machine to GitHub
- Latest changes from repo to your machine
- Add tracking information to your work
- What branch
- Create a local branch and push it to GitHub
- Create a PR [Pull Request]
- Check remotes
- Sync a remote fork on your machine
- Sync a remote fork on Github
- Add code on your machine to new repo
- Delete branches
- Merge master branch into feature branch
- Merge two repos
- Stop tracking a file
- Stop tracking a previously tracked folder
- Start tracking a previously un-tracked file
- Cloning a repo from someone else's GitHub and pushing it to a repo on my GitHub
- Remove an
- Clone a repo and give it a different name
- Using Husky?
- How to read last commit comment?
- Remove commit from pull request
- Conflicts between Windows Git and WSL Git?
- If you want to rename a branch while pointed to any branch, do:
- Git ref log
- Use SSH in place of HTTPS
- How to authenticate with GitHub using SSH
- Use multiple SSH keys
- Re-use SSH keys, from one machine to another
- Using SSH over the HTTPS port
- Specify multiple users for myself in .gitconfig?
- Cant remember what your last git commit said?
- Rebase changes
- Rebase accept incoming in bulk
- See differences between two branches
- See differences between two files
This is just stuff that I have put down that I find I use a lot of the time for my own reference.
Add a new repo from your machine to GitHub
echo "# name-of-your-awesome-repo" >> README.md # add repo name to README.md git init # init the repository git add README.md git commit -m "first commit" git remote add origin https://github.com/spences10/name-of-your-awesome-repo.git git push -u origin master
The first four commands can be ignored if you have done more work on the repo than adding a README.md
Latest changes from repo to your machine
Add tracking information to your work
Assuming that you are working on the master branch then
git branch --set-upstream-to=origin/master
You can set it to whatever branch you want to track changes for
git branch --set-upstream-to=origin/<branch>
This will mean you can just do
git pull and the latest changes will
be pulled to your
git branch # shows what branch you're on git branch -r # shows remote branches git branch -a # shows all branches
Create a local branch and push it to GitHub
Want to make your feature branch and get it on GitHub?
Make your branch first then:
git push --set-upstream origin <branch-you-just-created>
Create a PR [Pull Request]
Fork other users repo in GitHub, then clone to your machine.
git clone https://github.com/YourUserName/awesome-awesome-repo
Add the remote repo:
git remote add upstream https://github.com/OtherUserName/awesome-awesome-repo
Create your branch:
git branch your-awesome-branch
Check it out:
git checkout your-awesome-branch
If adding a folder use.
git add nameOfFolder/\\*
Make your commit and push to your new branch.
git add . git commit -m 'initial commit' git push origin your-awesome-branch
Manage the rest of the PR via GitHub
git remote -v
Sync a remote fork on your machine
First configure the local to point to the remote upstream
git remote -v git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git git remote -v git fetch upstream git checkout master git merge upstream/master
You then use
git merge to update any branch on the upstream
git merge upstream/dev
Take a look at syncing a fork for more details.
Sync a remote fork on Github
- Open your fork on GitHub.
- Click on Pull Requests.
- Click on New Pull Request. By default, GitHub will compare the original with your fork, and there shouldn't be anything to compare if you didn't make any changes.
- Click on Try
switching the base. Now GitHub will compare your fork with the original, and you should see all the latest changes.
- Click on Click to create a pull request for this comparison and assign a predictable name to your pull request (e.g., Update from original).
- Click on Send pull request.
- Scroll down and click Merge pull request and finally Confirm merge. If your fork didn't have any changes, you will be able to merge it automatically.
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.
git remote set-url origin https://yourgithubuser:firstname.lastname@example.org/yourgithubuser/yourrepo.git
If you want to change the origin url you can use the
git remote set-url origin https://github.com/user/new-repo-name
Add code on your machine to new repo
Via terminal navigate to your code folder.
Add your files.
git add .
Adding a folder use the following syntax or it'll get added as a BLOB.
git add nameOfFolder/\\*
Commit to local repo.
git commit -m 'some detailed message'
To add your files to the remote repo, first add your remote repo
git remote add origin [remote repository URL] # Sets the new remote git remote -v # Verifies the new remote URL git push origin master
For more info check out: adding an existing project to github using the command line
Delete local branch.
git branch -D branch-name
Remove local branches that are not on the
git remote prune origin
Remove local branches that were created from remote branches.
git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d
Merge master branch into feature branch
How to merge the master branch into the feature branch? This will come up often if you are workingon a team with other devs and you want to update your feature branch to include the latest changes.
# checkout your feature branch git checkout feature1 # merge master into it git merge master
Merge two repos
If you want to merge project-a into project-b:
cd path/to/project-b git remote add project-a path/to/project-a git fetch project-a git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge git remote remove project-a
Stop tracking a file
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:
git update-index --assume-unchanged <file>
Stop tracking a previously tracked folder
First add the folder to your
.gitignore then remove the folder from
your local git tracking with:
git rm -r --cached <folder>
Start tracking a previously un-tracked file
git update-index --no-assume-unchanged <file>
Cloning a repo from someone else's GitHub and pushing it to a repo on my GitHub
So you make a clone, make some changes then realise that you need to add it to your GitHub account before making a pull
git remote -v origin https://github.com/OtherUser/OtherUserRepo (fetch) origin https://github.com/OtherUser/OtherUserRepo (push)
You just need to set the
origin to yours then add the
origin make sense?
origin to yours:
git remote set-url origin http://github.com/YourUser/YourRepo
upsrtream as theirs:
git remote add upstream https://github.com/OtherUser/OtherUserRepo
Now it should look something like this:
git remote -v origin http://github.com/YourUser/YourRepo (fetch) origin http://github.com/YourUser/YourRepo (push) upstream https://github.com/OtherUser/OtherUserRepo (fetch) upstream https://github.com/OtherUser/OtherUserRepo (push)
If you no longer need a reference to a forked repository then remove it with the following:
git remote rm upstream
Clone a repo and give it a different name
git clone https://github.com/user/repoNameYouToChange NameYouWantToGiveToRepo
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
How to read last commit comment?
git show # is the fastest to type, but shows you the diff as well. git log -1 # is fast and simple. git log -1 --pretty=%B # if you need just the commit message and nothing else.
Remove commit from pull request
Read this for more detail on how to revert.
This was the simplest approach I found:
# Checkout the desired branch git checkout <branch> # Undo the desired commit git revert <commit> # Update the remote with the undo of the code git push origin <branch>
Rather than use the last part I unstaged the changes in VSCode which I think did the same thing.
There are there are three leves for Git config:
# to view git config --list --system # to set git config --system color.ui true
# to view git config --list --global # to set git config --global user.name xyz
# to view git config --list --local # to set git config --local core.ignorecase true # (--local optional) # to edit repository config file git config --edit --local # (--local optional)
View All Settings
git config --list --show-origin
Conflicts between Windows Git and WSL Git?
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:
git config --list --show-origin
Remove any conflicting settings then try again.
If you want to rename a branch while pointed to any branch, do:
git branch -m <oldname> <newname>
If you want to rename the current branch, you can do:
git branch -m <newname>
A way to remember this, is
-m is for "move" (or
mv), which is how
you rename files.
Git ref log
Want to know what work you have done on a repo? Use
git reflog to
displpay all the commits.
# show all changes for the last 90 days git reflog show -a # show changes with a date git reflog --date=iso
Use SSH in place of HTTPS
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
git remote set-url origin email@example.com:username/repo-name-here.git
Where username is the
username of the repo owner and
repo-name-here is the name of that user's repository.
How to authenticate with GitHub using SSH
Check that there are no
rsa files here before continuing, use (bash
or Git bash if you're on Windows):
ls -al ~/.ssh
If there's nothing there then generate a new keygen with:
ssh-keygen -t rsa -b 4096 -C firstname.lastname@example.org # add your email address 👍
If you decide to use a password for your SSH key see SSH Keys With Passwords
ls -al ~/.ssh will show our
Add the SSH key to the SSH agent:
# for mac and Linux from bash, also from Windows Git Bash eval "$(ssh-agent -s)" # for Git Bash on Windows eval `ssh-agent -s` # fir Fish shell eval (ssh-agent -c)
Add RSA key to SSH with:
Copy your key to clipboard with one of the following:
clip < ~/.ssh/id_rsa.pub # Windows cat ~/.ssh/id_rsa.pub # Linux pbcopy < ~/.ssh/id_github.pub # Mac
Then authenticate with:
ssh -T email@example.com
If you go back to the GitHub setting page and refresh the key icon
should go from black to green.
SSH Keys With Passwords
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
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 follwowinf to your
[ -z "$SSH_AUTH_SOCK" ] && eval "$(ssh-agent -s)"
~/.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
Use multiple SSH keys
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:
# ls ~/.ssh ~/.ssh/id_rsa_github_1 ~/.ssh/id_rsa_github_2 ~/.ssh/id_rsa_git_aws
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
file already set up with:
ls -al ~/.ssh/
If you haven't got a
config file there then:
cd ~/.ssh/ touch config
Use your text editor of choice, in this example we'll use
Add your configuration:
AddKeysToAgent yes # github_1 account Host github.com-github_1 HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_1 # github_2 account Host github.com-github_2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_2 # AWS code commit account Host git-codecommit.*.amazonaws.com User AWSUSERNAME IdentityFile ~/.ssh/id_rsa_git_aws
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.
Re-use SSH keys, from one machine to another
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 from Linux to Windows
cp ~/.ssh/* /c/Users/Scott.Spence/.linuxFiles/.ssh/ cp ~/.gitconfig /c/Users/Scott.Spence/.linuxFiles/
Copy from Windows to Linux
cp /mnt/c/Users/Scott.Spence/.linuxFiles/.ssh/* ~/.ssh/ # Reset the permissions back to default: sudo chmod 600 ~/.ssh/id_rsa sudo chmod 600 ~/.ssh/id_rsa.pub cp /mnt/c/Users/Scott.Spence/.linuxFiles/.* ~/ chmod 644 ~/.gitconfig
Start the SSH agent with:
eval "$(ssh-agent -s)" # for mac and Linux from bash, also from Windows Git Bash
Add your SSH key to the
Then authenticate with:
# GitHub ssh -T firstname.lastname@example.org # Bitbucket ssh -T email@example.com
Using SSH over the HTTPS port
SSH can be tunnelled over HTTPS if the network you are on blocks the SSH port.
Test if SSH over HTTPS is possible with:
ssh -T -p 443 firstname.lastname@example.org
If you get a response then, edit your
~/.ssh/config file and add
Host github.com Hostname ssh.github.com Port 443
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:
ssh -T email@example.com
Specify multiple users for myself in .gitconfig?
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:
g config --list
From the root of the repo, run:
git config user.name 'Your Name' git config user.email 'firstname.lastname@example.org'
Whereas the default user / email is configured in your
git config --global user.name 'Your Name' git config --global user.email 'email@example.com'
Cant remember what your last git commit said?
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
# switch from your feature to get latest develop changes git checkout develop git pull # checkout the feature branch and rebase git checkout feature git rebase develop
Then use the prompts from there in conjunction with your text editor to add in the changes.
# add a change git add # continue the rebase git rebase --continue # have an unrelated change, nothing to correct git rebase --skip # oh DERP! Want to start over? git rebase --abort
Rebase accept incoming in bulk
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.
# checkout the file git checkout temp-branch -- package-lock.json # add the file whilst in rebase git add package-lock.json # continue with the things git rebase --continue
See differences between two branches
If you want to see the difference between two branches then use the git built in diff tool.
git diff branch1..branch2
See differences between two files
If you want to see the difference between two file across different branches then use.
git diff branch1..branch2 package.json