To https://github.com/yourusername/repo.git ! [rejected] mybranch -> mybranch (non-fast-forward) error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards'in'git push --help'for details.
(my-branch)$ git status # On branch my-branch # Your branch is ahead of 'origin/my-branch' by 2 commits. # (use "git push" to publish your local commits) #
# On branch develop # Your branch is up-to-date with 'origin/develop'. # Changes to be committed: # (use "git reset HEAD ..." to unstage) ##
modified: file1.txt
然后,正常提交。
Note: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem.
我把几个提交(commit)提交到了同一个分支,而这些提交应该分布在不同的分支里
假设你有一个main分支, 执行git log, 你看到你做过两次提交:
(main)$git log commite3851e817c451cc36f2e6f3049db528415e3c114 Author: Alex Lee Date: Tue Jul 22 15:39:27 2014 -0400 Bug#21 - Added CSRF protection commit5ea51731d150f7ddc4a365437931cd8be3bf3131 Author: Alex Lee Date: Tue Jul 22 15:39:12 2014 -0400 Bug#14 - Fixed spacing on title commita13b85e984171c6e2a1729bb061994525f626d14 Author: Aki Rose Date: Tue Jul 21 01:12:48 2014 -0400 Firstcommit
让我们用提交hash(commit hash)标记bug (e3851e8 for #21, 5ea5173 for #14).
(my-branch)$ git checkout main Switched to branch 'main'Your branch is up-to-date with 'origin/main'. (main)$ git branch -D my-branch Deleted branch my-branch (was 4e3cd85). (main)$ echo oh noes, deleted my branch! oh noes, deleted my branch!
在这时候你应该想起了reflog, 一个升级版的日志,它存储了仓库(repo)里面所有动作的历史。
(main)$ git reflog 69204cd HEAD@{0}: checkout: moving from my-branch to main 4e3cd85 HEAD@{1}: commit: foo.txt added 69204cd HEAD@{2}: checkout: moving from main to my-branch
(main)$ git checkout -b my-branch-helpSwitched to a new branch 'my-branch-help'(my-branch-help)$ git reset--hard 4e3cd85 HEADisnowat4e3cd85 foo.txt added (my-branch-help)$ ls README.md foo.txt
看!我们把删除的文件找回来了。Git的 reflog 在rebasing出错的时候也是同样有用的。
我想删除一个分支
删除一个远程分支:
(main)$ git push origin --deletemy-branch
你也可以:
(main)$ git push origin :my-branch
删除一个本地分支:
(main)$ git branch -D my-branch
我想从别人正在工作的远程分支签出(checkout)一个分支
首先,从远程拉取(fetch) 所有分支:
(main)$ git fetch --all
假设你想要从远程的daves分支签出到本地的daves
(main)$ git checkout --track origin/daves Branch daves set up to track remote branch daves from origin. Switched to a new branch 'daves'
在你执行了交互式 rebase的命令(interactive rebase command)后, 你将在你的编辑器里看到类似下面的内容:
picka9c8a1d Some refactoring pick01b2fd8 New awesome feature pickb729ad5 fixup picke3851e8 another fix # Rebase 8074d12..b729ad5 onto 8074d12 # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
picka9c8a1d Some refactoring pick01b2fd8 New awesome feature sb729ad5 fixup se3851e8 another fix
你可以在接下来弹出的文本提示框里重命名提交(commit)。
Newer, awesomer features # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # rebase in progress; onto 8074d12 # You are currently editing a commit while rebasing branch 'main' on '8074d12'. # # Changes to be committed: # modified: README.md #
如果成功了, 你应该看到类似下面的内容:
(main)$ Successfully rebased and updated refs/heads/main.
(my-branch)$ git status On branch my-branch Changes not staged for commit: (use"git add ..."toupdate what will be committed) (use"git checkout -- ..."to discard changes in working directory) modified: README.md 在这个例子里面, README.md 有冲突。打开这个文件找到类似下面的内容: <<<<<<< HEADsome code ========= some code >>>>>>> new-commit
$ git config --global credential.helper cache # Set git to use the credential memory cache $ git config --global credential.helper 'cache --timeout=3600'# Set the cache to timeout after 1 hour (setting is in seconds)
这就是git reflog 的目的, reflog 记录对分支顶端(the tip of a branch)的任何改变, 即使那个顶端没有被任何分支或标签引用。基本上, 每次HEAD的改变, 一条新的记录就会增加到reflog。遗憾的是,这只对本地分支起作用,且它只跟踪动作 (例如,不会跟踪一个没有被记录的文件的任何改变)。
(main)$ git reflog 0a2e358 HEAD@{0}: reset: moving to HEAD~20254ea7 HEAD@{1}: checkout: moving from 2.2 to main c10f740 HEAD@{2}: checkout: moving from main to 2.2