Skip to content

Instantly share code, notes, and snippets.

@jimbrig
Last active October 13, 2020 22:45
Show Gist options
  • Select an option

  • Save jimbrig/bac9a240e081099e27279941be594b55 to your computer and use it in GitHub Desktop.

Select an option

Save jimbrig/bac9a240e081099e27279941be594b55 to your computer and use it in GitHub Desktop.
[Useful Git Commands] A listing of common git commands I use #git #version-control #github
  • If you fix a bug or create a new feature – do it in a separate branch!
git clone git://github.com/jimbrig/repo.git
cd repo
git checkout -b bugfix_branch

Before merging or starting a PR, consider creating a patch!

Creating a patch

First, check the log of the branch:

git-log --pretty=oneline -3

Here's how to create a patch:

git format-patch master --stdout > fix_bug.patch

This will create a new file fix_bug.patch with all changes from the current (bugfix_branch) against master. Normally, git would create a separate patch file for each commit, but that’s not what we want. All we need is a single patch file.

Apply Patch

First, take a look at what changes are in the patch. You can do this easily with git apply

git apply --stat fix_bug.patch

Note that this command does not apply the patch, but only shows you the stats about what it’ll do. After peeking into the patch file with your favorite editor, you can see what the actual changes are.

Next, you’re interested in how troublesome the patch is going to be. Git allows you to test the patch before you actually apply it.

git apply --check fix_bug.patch

To apply the patch, I’ll use git am instead of git apply.

The reason for this is that git am allows you to sign off an applied patch.

This may be useful for later reference.

git am --signoff < fix_empty_poster.patch
Applying: Added specs to test empty poster URL behaviour
Applying: Added poster URL as part of cli output

Useful Git commands

Latest changes from repo to your machine

$ git pull

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 origin

What branch?

$ git branch shows what branch you're on

$ git branch -r shows remote branches

$ git branch -a shows all branches

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

Check remotes

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 repository:

$ git merge upstream/dev

Sync a remote fork on Github

  1. Open your fork on GitHub.
  2. Click on Pull Requests.
  3. 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.
  4. Click on Try switching the base. Now GitHub will compare your fork with the original, and you should see all the latest changes.
  5. 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).
  6. Click on Send pull request.
  7. 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.

2fa

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:[email protected]/yourgithubuser/yourrepo.git

Change origin url

If you want to change the origin url you can use the set-url command

git remote set-url origin https://github.com/user/new-repo-name

Via terminal navigate to your code folder.

$ git init

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

Delete local branch

$ git branch -D use-dotenv

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>

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 upstream as the original origin make sense?

So change origin to yours:

$ git remote set-url origin http://github.com/YourUser/YourRepo

Then add 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)

Clone a repo and give it a different name

$ git clone https://github.com/user/repoNameYouToChange NameYouWantToGiveToRepo

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.

Show .gitconfig details

git config --list --show-origin

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.

Add a repo from your machine to GitHub

Create a new repo and push it 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/your-username/name-of-your-awesome-repo.git
git push -u origin master

The first four commands can be ignored if you have a repo you're already working on (git)committing to.

Latest changes from repo to your machine

git pull

Add tracking information to your work

Assuming that you are working on the master branch then

git branch --set-upstream-to=origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment