Basic usage of git.

Squashing Git Commits

https://gist.github.com/patik/b8a9dc5cd356f9f6f980

git reset --soft HEAD~3
git commit -m "Squashing Git Commits"
git push origin +master
git log --author=songzy
git reset f7f4aec68
git log -2
git reset 7e26106
git log -2
git clean -df
git checkout -- .

git reset

git reset <file>

Remove the specified file from the staging area, but leave the working directory unchanged.

git reset

Reset the staging area to match the most recent commit, but leave the working directory unchanged.

git reset --hard

Reset the staging area and the working directory to match the most recent commit.

git reset <commit>

Move the current branch tip backward to , reset the staging area to match, but leave the working directory alone.

git reset --hard <commit>

Move the current branch tip backward to and reset both the staging area and the working directory to match.

Whereas reverting is designed to safely undo a public commit, git reset is designed to undo local changes.

git add foo.py
git commit -m "Start developing a crazy feature"
git commit -a -m "Continue my crazy feature"
git reset --hard HEAD~2

The git reset HEAD~2 command moves the current branch backward by two commits.

git rebase

# Add the remote, call it "upstream":

git remote add upstream https://github.com/whoever/whatever.git

# Fetch all the branches of that remote into remote-tracking branches,
# such as upstream/master:

git fetch upstream

# Make sure that you're on your master branch:

git checkout master

# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:

git rebase upstream/master

git push -f origin master

git branch

git show-branch -a

Show branches and their commits

git branch <branch>

Create a new branch called . This does not check out the new branch.

git branch -d <branch>

Delete the specified branch.

git branch -D <branch>

Force delete the specified branch, even if it has unmerged changes.

git branch -m <branch>

Rename the current branch to <branch>.

git checkout $branch2
git merge $branch1

Merge branch1 into branch2.

warning: refname 'HEAD' is ambiguous.
git branch -m HEAD temp
git branch -d temp

git checkout

Give up the current modification and go back to the last commit

git checkout -- .
git log --oneline

b7119f2 Continue doing crazy things
872fa7e Try something crazy
a1e8fb5 Make some important changes to hello.py
435b61d Create hello.py
9773e52 Initial import

You can find the ID of the revision you want to see.

git checkout a1e8fb5
git checkout master

Nothing you do in here will be saved in your repository.

git checkout a1e8fb5 hello.py
git checkout HEAD hello.py

Unlike checking out a commit, this does affect the current state of your project.


git status
HEAD detached From 91ea20b
git checkout source
error: Your local changes to the following files would be overwritten by checkout:
        source/_posts/2016-02-25-github.markdown
Please, commit your changes or stash them before you can switch branches.
Aborting
git stash
git checkout source

git clean

git clean -n

This will show you which files are going to be removed without actually doing it.

git clean -f

Remove untracked files from the current directory.

git clean -f <path>

Remove untracked files, but limit the operation to the specified path.

git clean -df

Remove untracked files and untracked directories from the current directory.

git clean -xf

Remove untracked files from the current directory as well as any files that Git usually ignores.

git clone

If you clone a repository, the default branch you have is whatever the remote’s HEAD points to (HEAD is actually a symbolic ref that points to a branch name).

A symbolic ref is a regular file that stores a string that begins with ref: refs/. For example, your .git/HEAD is a regular file whose contents is ref: refs/heads/master.

HEAD@{1} is always last value of HEAD, ORIG_HEAD is last value of HEAD before dangerous operation.

git commit

git commit --amend 

Combine the staged changes with the previous commit and replace the previous commit with the resulting snapshot.

git commit -a

Commit all your local changes. (like git add . followed by git commit)

git config

vi ~/.gitconfig
git config --global user.email "your_email@example.com"
git config --global user.email

git diff

git diff filename

git fetch

git fetch

Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.

git checkout master
git log origin/master
git merge origin/master

error: RPC failed; result=18, HTTP code = 0
git fetch origin master
git merge origin/master

git format-patch

$ git format-patch -o ./patches -2
0001-C.patch
0002-D.patch
git am ./patches/*

git log

Check all the change paths to file Predictor.java.

git log --follow -p Predictor.java
git log -p -1

git merge

git merge --squash <branch_name>

將另一個 branch 的 commit 合併為一筆,特別適合需要做實驗的 fixes bug 或 new feature,最後只留結果。合併完不會幫你先 commit。

git pull

git pull <远程主机名> <远程分支名>:<本地分支名>

如果远程分支是与当前分支合并,则冒号后面的部分可以省略。

Here.


ssh: connect to host github.com port 22: Connection refused
vi ~/.ssh/config

Host github.com
  Hostname ssh.github.com
  Port 443

git push

git push <远程主机名> <本地分支名>:<远程分支名>
git push origin HEAD:master

See the commits tab(rather than project tab). It is all there.

ssh -v -T songzy12@git.tsinghuax.org

IP filtered, move to your lab.

fatal: Could not read from remote repository.

Server down.

git remote

git remote set-url origin git@github.com:songzy12/certificate_predictor.git

git revert

git revert <commit>

Generate a new commit that undoes all of the changes introduced in <commit>, then apply it to the current branch.

  • it doesn’t change the project history
  • it is able to target an individual commit at an arbitrary point in the history
git commit -m "Make some changes that will be undone"
git revert HEAD

git rm

git rm -r --cached ".\source\_posts\Git Shell.lnk"

The git rm command will allows you to remote a file from git control. The –cached option to git remove allows you to leave it on your hard drive.

git stash

git stash
git stash list
git stash pop
git stash clear
git stash drop

fileMode

  • 100644: normal file
  • 100755: executable file
  • 120000: symbolic link

The mode is taken from normal UNIX modes but is much less flexible – these three modes are the only ones that are valid for files (blobs) in Git (although other modes are used for directories and submodules).

The 6 digits show the file mode using the classical UNIX notations. First two digits show file type, the third one is about set-uid/set-gid/sticky bits, and you know the last three. 4\2\1 is just r\w\x.

man 2 stat
       S_IFLNK    0120000   symbolic link
       S_IFREG    0100000   regular file

       S_IRWXU    00700     mask for file owner permissions
       S_IRUSR    00400     owner has read permission
       S_IWUSR    00200     owner has write permission
       S_IXUSR    00100     owner has execute permission
       S_IRWXG    00070     mask for group permissions
       S_IRGRP    00040     group has read permission
       S_IWGRP    00020     group has write permission
       S_IXGRP    00010     group has execute permission
       S_IRWXO    00007     mask for permissions for others (not in group)
       S_IROTH    00004     others have read permission           
       S_IWOTH    00002     others have write permission
       S_IXOTH    00001     others have execute permission

To ignore file mode changes:

repo forall -c git config core.fileMode false