Skip to content

Instantly share code, notes, and snippets.

@ssledz
Last active January 22, 2020 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssledz/a152b269a77ab2fe22e8bf3c642f7baf to your computer and use it in GitHub Desktop.
Save ssledz/a152b269a77ab2fe22e8bf3c642f7baf to your computer and use it in GitHub Desktop.

Making Commits

changing the index

$ git add filename  # adding new file; adding changes to the existing file
$ git add -p        # adding partial changes; git add --patch (-p)
$ git add -u        # include all files in the current index; this includes changed and deleted files, 
                    # but not new ones.
$ git add -A        # include all filenames in the index and in the working tree; this stages new 
                    # files as well
$ git rm filename   # removing file
$ git mv foo bar    # renaming file
$ git reset         # resets the index to match the current commit, undoing any changes you’ve made 
                    # with git add (unstage all the changes)
# git reset -p      # reset partial changes; dual to git add -p; git reset --patch

making commit

git commit -m "commit message"    # 
git commit -a                     # equivalent to git add -u followed by git commit

undoing & editing commits

$ git commit --amend          # discards the previous commit and puts a new one in its place
$ git commit --amend -C HEAD  # ^ + reuse the previous commit
$ git reset HEAD~             # discarding the last commit; moves the head of the current branch 
                              # one commit behind; updates the index but leaves the working tree alone
$ git reset HEAD~3            # discards three commits resetting the branch tip to the fourth commit 
            ^   ^             # back (HEAD == HEAD~0 i HEAD~ == HEAD~1)
            |   trailing tilde names the commit prior to the one pointed by the ref (in this case HEAD)
            HEAD ref refers to the tip of current branch
            
$ git revert commit           # computes the diff between that commit and the previous one, reverse it, 
                              # and then attempt to apply that to your working tree; git will prepare 
                              # a commit message indicating the commit being reverted and its subject
$ git rebase -i HEAD~n        # rewrites the last n commits on the current branch

partial undo

$ git revert -n commit  # apply and stage the reverted changes, but stop short of making a commit
$ git reset             # unstage all the changes
$ git add -p            # adding partial changes
$ git commit            # committing the subset of changes
$ git checkout .        # discard the rest by checking out the contents of the index

git reset

Has several modes and actions. It always moves the head of the current branch to a given commit, but differs in how it treats the working tree and index.

  • --mixed - The default: makes the index match the given commit, but does not change the working files. Changes made since the last commit appear unstaged.
  • --soft - This resets the branch tip only, and does not change the index; the discarded commit’s changes remain staged. You might use this to stage all the changes from several previous commits, and then reapply them as a single commit.
  • --merge - Tries to keep your outstanding file changes while rewinding the branch, where this makes sense: files with unstaged changes are kept, while files differing between HEAD and the given commit are updated. If there is overlap between those sets, the reset fails.
  • --hard - Resets your working files to match the given commit, as well as the index. Any changes you’ve made since the discarded commit are permanently lost, so be careful with this option! Resist the urge to make an alias or shortcut for using git reset --hard; you will probably regret it.

git rebase

Rebasing is a general technique intended to move a branch from one location to another

  • pick - Use the commit as-is. Git will not stop for this commit unless there is a conflict.
  • reword - Change just the commit message. Git allows you to edit the message before reapplying this commit.
  • edit - Change the commit contents (and message, if you want). Here, Git stops after remaking this commit and allows you to do whatever you want. The usual thing is to use git commit --amend to replace the commit, then git rebase --continue to let Git continue with the rebase operation. However, you could also insert further commits, perhaps splitting the original changes up into several smaller commits. Git simply picks up from where you leave off, with the next change you asked it to make.
  • squash - Make this commit’s changes part of the preceding one. To meld several consecutive commits into one, leave the first one marked pick and mark the remaining ones with squash. Git concatenates all the commit messages for you to edit.
  • fixup - Like squash, but discard the message of this commit when composing the composite message.

commit workflow

  1. Use git add (with various options) to stage a subset of your changes.
  2. Run git stash --keep-index. This saves and undoes your outstanding, unstaged changes while preserving your staged changes in the index, and resets your working tree to match the index.
  3. Examine this working tree state to make sure your selection of changes makes sense; build and test your software, for example.
  4. Run git commit.
  5. Now, use git stash pop to restore your remaining unstaged changes, and go back to step 1.
  6. Continue this process until you’ve committed all your changes, as confirmed by git status reporting “nothing to commit, working directory clean.”

Branching

$ git init                          # creates a new empty git repository
$ git checkout -b develop           # creates the branch develop pointing at the current commit, 
                                    # and switches to it
$ git checkout -b feature 9c6a1fad  # ^ + specify a commit at which to start the new branch
$ git checkout develop              # switches to develop branch
$ git branch -d feature
$ git branch -vv                    # list upstream branches
$ git push origin :feature          # deleting the branch from the origin repository
$ git branch -m old new             # renames a local branch

renaming branch flow

$ git branch -m old new           # rename a branch locally
$ git push -u origin new          # push a new branch to origin
$ git push origin :old            # remove old branch from origin

Viewing history

$ git log -g   # displays a composite reflog
e674ab77 HEAD@{0}: commit (amend): Digital Restrictio…
965dfda4 HEAD@{1}: commit: Digital Rights Management
dd31deb3 HEAD@{2}: commit: Mozart
3307465c HEAD@{3}: commit: Beethoven

Other

use diffrent ssh key during clone repo

ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment