Skip to content

Instantly share code, notes, and snippets.

@vikbert
Last active February 13, 2019 09:50
Show Gist options
  • Save vikbert/2e0b25813ae54eecc194a4cc1ffe5f6d to your computer and use it in GitHub Desktop.
Save vikbert/2e0b25813ae54eecc194a4cc1ffe5f6d to your computer and use it in GitHub Desktop.
[git cheatsheet] GIT common used commands #git, #cheatsheet

GitHub

git glossar

git-rebase

reapply all commits from your branch to the TOP of another branch. It will sequentially take all the commmits from the branch you're in, and reapply them to the destination branch. Git-rebase does not destroy the old ones. git rebase

read more git-rebase golden rule

General Git helpers

dotfiles

Commit Messages

Lean package "enforcement"

git config

user]
	name = Xun Zhou
	email = xun.zhou@lidl.com

[core]
  autocrlf = input
  excludesfile = ~/.gitignore

[diff]
  tool = icdiff

[difftool]
  prompt = false

[difftool "icdiff"]
  cmd = /usr/local/bin/icdiff --line-numbers $LOCAL $REMOTE

[icdiff]
	options = --highlight --line-numbers
[filter "lfs"]
	clean = git-lfs clean -- %f
	smudge = git-lfs smudge -- %f
	process = git-lfs filter-process
	required = true

PR conflict with develop

git rebase origin/develop

Rename

# in the branch which you wanna rename
git branch -m new_branch_name

How to contribution

  • Fork it!
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request :D

Create a new repository on the command line

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:alexpchin/<reponame>.git
git push -u origin master

Push an existing repository from the command line

git remote add origin git@github.com:alexpchin/<reponame>.git
git push -u origin master

How to add the changes to previous old commit

Use git rebase

  • Use git stash to store the changes you want to add.
  • Use git rebase -i HEAD~10 (or whatever you need to see far enough back).
  • Mark the commit for edit by changing the word pick at the start of the line into edit. Don't delete the other lines as that would delete the commits.
  • Save the rebase file, and git will drop back to the shell and wait for you to fix that commit.
  • Pop the stash by using git stash pop
  • Add your file with git add .
  • Amend the commit with git commit --amend
  • Do a git rebase --continue which will rewrite the rest of your commits against the new one

There are 3 levels of git config; project, global and system.

  • project: Project configs are only available for the current project and stored in .git/config in the project's directory.
  • global: Global configs are available for all projects for the current user and stored in ~/.gitconfig.
  • system: System configs are available for all the users/projects and stored in /etc/gitconfig.

Create a project specific config, you have to execute this under the project's directory.

$ git config user.name "John Doe"

Create a global config

$ git config --global user.name "John Doe"

Create a system config

$ git config --system user.name "John Doe"`

my git cheatsheet

# git Vim conflict
git config --global core.editor /usr/bin/vim

# updated local branches with remote branches
git remote prune origin

# push local feature/ci to remote server
git push -u origin feature/ci

# replace master with develop
git checkout develop && git pull
git merge -s ours master
git checkout master
git merge develop && git push

# ammend commit without edit msg
git commit --amend --no-edit

重设第一个commit

也就是把所有的改动都重新放回工作区,并清空所有的commit,这样就可以重新提交第一个commit了

git update-ref -d HEAD

切换分支

git checkout -

把A分支的某一个commit,放到 develop 分支上

git checkout A
git add foo.txt 
git commit -m 'added foo.txt' # c8cad1e21f54a2dd3efaae4a92c8affd8aa99999

git checkout develop
git cherry-pick c8cad1e21f54a2dd3efaae4a92c8affd8aa99999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment