Undo a commit and redo

$ git commit -m "Something terribly misguided"
$ git reset --soft HEAD~
<< edit files as necessary >>
$ git add ...
$ git commit -c ORIG_HEAD

Syncing a fork

$ git fetch upstream
$ git checkout master
$ git merge upstream/master

Setting the upstream for a fork

$ git remote -v
origin  https://github.com/nohe427/developer-support.git (fetch)
origin  https://github.com/nohe427/developer-support.git (push)

$ git remote add upstream https://github.com/esri/developer-support

$ git remote -v
origin  https://github.com/nohe427/developer-support.git (fetch)
origin  https://github.com/nohe427/developer-support.git (push)
upstream        https://github.com/esri/developer-support (fetch)
upstream        https://github.com/esri/developer-support (push)

Git rm all untracked files

$ git clean -df

Discard unstaged changes

$ git stash save --keep-index

After that, you can drop that stash with a git stash drop command if you like.

or

$ git clean -dfx
$ git checkout .

Unstage multiple files

If all you want is to undo an overzealous “git add” run:

$ git reset

Your changes will be unstanged and ready for you to re-add as you please.

Rebase a specific commit

To rebase to a specific commit, not to a HEAD of the other branch:

A --- B --- C          master
 \
  \-- D                topic

to

A --- B --- C          master
       \
        \-- D          topic

instead of

A --- B --- C          master
             \
              \-- D    topic
$ git branch temp master^
$ git checkout topic
$ git rebase temp
$ git branch -d temp

Merging vs. Rebasing

Further Reading: Git 分支 - 分支的衍合

一旦分支中的提交物件發佈到公共倉庫,就千萬不要對該分支進行衍合操作。

Remove a folder from git tracking

#1. Add the folder path to your repo’s root .gitignore file.

path_to_your_folder/

#2. Remove the folder from your local git tracking, but keep it on your disk.

$ git rm -r --cached path_to_your_folder/

#3. Push your changes to your git repo.

The folder will be considered “deleted” from Git’s point of view (i.e. they are in past history, but not in the latest commit, and people pulling from this repo will get the files removed from their trees), but stay on your working directory because you’ve used --cached.

Create a new branch with git and manage branches

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

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

Before creating a new branch, pull the changes from upstream. Your master needs to be up to date.

Create the branch on your local machine and switch in this branch :

$ git checkout -b [name_of_your_new_branch]

Push the branch on github :

$ git push origin [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 -a

Add a new remote for your branch :

$ git remote add [name_of_your_remote]

Push changes from your commit into your branch :

$ git push [name_of_your_new_remote] [name_of_your_branch]

Update your branch when the original branch from official repository has been updated :

$ git fetch [name_of_your_remote]

Then you need to apply to merge changes, if your branch is derivated from develop you need to do :

$ git merge [name_of_your_remote]/develop

Delete a branch on your local filesystem :

$ git branch -d [name_of_your_new_branch]

To force the deletion of local branch on your filesystem :

$ git branch -D [name_of_your_new_branch]

Delete the branch on github :

$ git push origin :[name_of_your_new_branch]

The only difference is the : to say delete, you can do it too by using github interface to remove branch : https://help.github.com/articles/deleting-unused-branches.

Get changes from master into branch in Git

In my repository I have a branch called aq which I’m working on. I then committed new work and bugs in master.

What is the best way to get those commits into the aq branch? Create another new branch out of master and merge it with aq?

Solution: Check out the aq branch, and rebase from master.

$ git checkout aq
$ git rebase master

Read More