Skip to content

Instantly share code, notes, and snippets.

@xtbl
Last active September 17, 2020 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtbl/6799749 to your computer and use it in GitHub Desktop.
Save xtbl/6799749 to your computer and use it in GitHub Desktop.
Git Cheatsheet and Guides

Good guides

A Hacker’s Guide to Git

Resolving merge conflicts in Git

Git from the inside out

Git Aliases

How to undo 'almost' anything with Git

Create a new repository

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/xtbl/mountain-bike-prime.git
git push -u origin master

Push an existing repository

git remote add origin https://github.com/xtbl/mountain-bike-prime.git
git push -u origin master

Clone from another repo and change remotes

git remote -v
git remote rm origin
git remote add origin https://github.com/xtbl/angular2test.git

Create a new branch with git and manage branches

https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches

In your github fork, you need to keep your master branch clean, I mean by clean without any changes, like that you can create at any time a branch from your master. Each time, that you want commit a bug or a feature, you need to create a branch for it, which will be somehow the copy of your master branch.

When you will do a pull request on a branch, you can continue to work on an another branch and make an another pull request on this other branch.

Before create a new branch pull the changes from upstream, your master need to be uptodate.

Create the branch on your local machine :

$ git branch name_of_your_new_branch

Push the branch on github :

$ git push origin name_of_your_new_branch 

Switch to your new branch :

$ git checkout name_of_your_new_branch 

When you want to commit something in your branch, be sure to be in your branch.

You can see all branches created by using

$ git branch 

Which will show :

* approval_messages
  master
  master_clean

Add a new remote for your branch :

$ git remote add name_of_your_remote  

Push changes from your commit into your branch :

$ git push origin name_of_your_remote 

Delete a branch on your local filesytem :

$ git branch -d name_of_your_new_branch

Delete the branch on github :

$ git push origin :name_of_your_new_branch

The only difference it's the : to say delete.

If you want to change default branch, it's so easy with github, in your fork go into Admin and in the drop-down list default branch choose what you want.

Git create a branch from another branch

$ git checkout -b myFeature dev

Creates MyFeature branch off dev. Do your work and then

$ git commit -am "Your message"

Now merge your changes to dev without a fast-forward

$ git checkout dev
$ git merge --no-ff myFeature

edit

Now push changes to the server

$ git push origin dev
$ git push origin myFeature

And you'll see it how you want it.

What is the difference between “git branch” and “git checkout -b”?

git checkout -b BRANCH_NAME creates a new branch and goes to the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch.

Git rename branch

$ git branch -m oldname newname

If you want to rename the current branch:

$ git branch -m newname

You asked me to pull without telling me which branch you want to merge with

http://stackoverflow.com/questions/7388278/you-asked-me-to-pull-without-telling-me-which-branch-you-want-to-merge-with

The simplest way to set up the association between your bucket-4 and bucket-4 in origin is to make sure that the next time you push, you do:

git push -u origin bucket-4

Alternatively, you can do:

git branch --set-upstream bucket-4 origin/bucket-4

=== .git/config

cat .git/config

Under [branch "master"], try adding the following to the repo's Git config file (.git/config):

[branch "master"] remote = origin merge = refs/heads/master This tells Git 2 things:

When you're on the master branch, the default remote is origin. When using git pull on the master branch, with no remote and branch specified, use the default remote (origin) and merge in the changes from the master branch. I'm not sure why this setup would've been removed from your configuration, though. You may have to follow the suggestions that other people have posted, too, but this may work (or help at least).

If you don't want to edit the config file by hand, you can use the command-line tool instead:

$ git config branch.master.remote origin $ git config branch.master.merge refs/heads/master

Switching a branch after aborting current changes in git

 $ git checkout gh-pages
error: Your local changes to the following files would be overwritten by checkout:
        somefile.txt
Please, commit your changes or stash them before you can switch branches.

Option 1

git checkout -f gh-pages

Option 2

git reset --hard     # beware: don't make that a habit
git checkout gh-pages

revert (reset) a single file

This one is hard to find out there so here it is. If you have an uncommitted change (its only in your working copy) that you wish to revert (in SVN terms) to the copy in your latest commit, do the following:

git checkout filename

prune branching

Deletes all stale tracking branches under . These stale branches have already been removed from the remote repository referenced by , but are still locally available in "remotes/".

git remote prune origin

cool delete branches trick

remove local branches

git branch --merged >/tmp/merged-branches && vi /tmp/merged-branches && xargs git branch -d </tmp/merged-branches

List all the files for a commit in Git

One way (preferred):

$ git diff-tree --no-commit-id --name-only -r bd61ad98
index.html
javascript/application.js
javascript/ie6.js

Another way:

$ git show --pretty="format:" --name-only bd61ad98
index.html
javascript/application.js
javascript/ie6.js

The --no-commit-id suppresses the commit ID output. The --pretty argument specifies an empty format string to avoid the cruft at the beginning. The --name-only argument shows only the file names that were affected

How to undo the last Git commit?

$ git commit ...              (1)
$ git reset --soft "HEAD^"    (2)
$ edit                        (3)
$ git add ....                (4)
$ git commit -c ORIG_HEAD     (5)
This is what you want to undo

This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset". (The quotes are required if you use zsh)

Make corrections to working tree files.

Stage changes for commit.

"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
How to undo the last Git commit?

Git revert a merge commit (missing -m error)

 git revert -m 1 HEAD 

why-does-git-revert-complain-about-a-missing-m-option

Git Commit to an existing Tag

  1. Create a branch with the tag
	git branch {tagname}-branch {tagname}
	git checkout {tagname}-branch
  1. Include the fix manually if it's just a change ....
	git add .
	git ci -m "Fix included"

or cherry-pick the commit, whatever is easier

    
	git cherry-pick  {num_commit}
  1. Delete and recreate the tag locally
	git tag -d {tagname}
	git tag {tagname}
  1. Delete and recreate the tag remotely
	git push origin :{tagname} // deletes original remote tag
	git push origin {tagname} // creates new remote tag

Git Delete Last Commit

If you have committed junk but not pushed,

git reset --soft HEAD~1

HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. --soft option will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

If you want to get rid of any changes to tracked files in the working tree since the commit before head use --hard instead. Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

git revert HEAD

Git unstage committed files

  1. http://stackoverflow.com/questions/6682740/how-can-i-unstage-my-files-again-after-making-a-local-commit
  2. http://stackoverflow.com/questions/348170/how-to-undo-git-add-before-commit http://stackoverflow.com/a/682343/618934
git reset --soft HEAD^
git diff --cached
git reset HEAD .

git add only modified changes and ignore untracked files

Ideally your .gitignore should prevent the untracked ( and ignored )files from being shown in status, added using git add etc. So I would ask you to correct your .gitignore

You can do

git add -u
so that it will only stage the modified files.

You can also do

git commit -a
to commit only the modifications.

Note that if you used git add ., then you would need to use git add -u . (See "Difference of “git add -A” and “git add .”"). http://stackoverflow.com/a/16162511/6309

unmerged files? want to discard current changes?

git reset --hard hash_of_last_commit_where_you_want_to_start

or

git stash save --keep-index

then

git stash drop

How can I merge two branches without losing any files?

git checkout a (you will switch to branch a)
git merge b (this will merge all changes from branch b into branch a)
git commit -a (this will commit your changes)

git checkout remote branch

git fetch origin

This will fetch all the remote branches, then:

git checkout -b test origin/test

Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.

To fetch a branch, you simply need to:

git fetch origin This will fetch all of the remote branches for you. You can see the branches available for checkout with:

git branch -v -a With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:

git checkout -b test origin/test EDIT - The answer below actually improves on this. On Git>=1.6.6 you can just do:

git fetch git checkout test

Commit message guidelines

With Add, Mod(ify), Ref(actoring), Fix, Rem(ove) and Rea(dability) then it's easy to extract logfile.

Example :

Add: New function to rule the world.
Mod: Add women factor in Domination.ruleTheWorld().
Ref: Extract empathy stuff to an abstract class.
Fix: RUL-42 or #42 Starvation need to be initialised before Energy to avoid the nullpointer in People.
Rem: freeSpeech is not used anymore.
Rea: Removed old TODO and extra space in header.

If I have more than a line, I sort them with most important first.

On undoing, fixing, or removing commits in git

On undoing, fixing, or removing commits in git

Pushing to two git remote origins from one repository

details here

$ git remote add backup user@server:/path/to/git/XML-to-text-Tomboy.git
$ git push backup master:master
$ ~/XML-to-text-Tomboy vi .git/config
[remote "all"]
url = git@github.com:danakim/XML-to-text-Tomboy.git
url = user@server:/path/to/git/XML-to-text-Tomboy.git
$ $ git push all

Git push to multiple servers

details here

$ git remote add github https://github.com/boynux/myrepo.git
$ git push github master

view unpushed commits

$ git log origin/master..HEAD

You can also view the diff using the same syntax

$ git diff origin/master..HEAD

Git stash

$ git stash save "guacamole sauce WIP"

or

$ git stash save
$ git stash list
$ git stash apply stash@{0}

or

$ git stash stash@{stash_number_here}
$ git stash show

to apply a stash and remove it from the stash stack:

$ git stash pop stash@{n}

Undo a commit and redo

$ git commit -m "Something terribly misguided"              (1)
$ git reset HEAD~                                           (2)
<< edit files as necessary >>                               (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)

Undo a commit and redo

How to see the changes in a commit?

git show COMMIT
or
git diff COMMIT^ COMMIT

How to see the changes in a commit?

Git Tags

git tag -a 0.58.0 -m "changelog"
git push origin tag_name 

Cherry pick

git cherry-pick -x sha_here 

Git squash

git rebase --interactive HEAD~[7] 

and

git rebase --interactive 6394dc 

Where 6394dc is Feature Y. You can read the whole thing as: "Merge all my commits on top of commit [commit-hash]."

https://www.internalpointers.com/post/squash-commits-into-one-git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment