Git Cheat Sheet

Useful Commands

git diff <origin/branch>...HEAD - what is waiting to be pushed

git diff HEAD - what is not committed and not in the index

git diff --cached - what is in the index but not committed

 

git log <origin/branch>...HEAD - commits to be pushed

git diff <origin/branch>...HEAD - diff to be pushed


git push origin <local-branch>:<remote-branch> - push between branches

git push origin <local-and-remote-branch> - push to remote branch with same name

git push -u origin <feature_branch_name> - push a new branch (and start tracking it)

git push origin HEAD - push only the current branch


git branch -a - list branches


git checkout -b <branch-name> - create new branch

git checkout <branch-name> - open branch


git add -A - add all files (better to have .gitignore first to exclude eclipse config/build files)


git commit -m “Commit message”  - commit added files (git add)


New Repository

 

git init - create repository

 

Utils

git diff -U30 file1.txt file2.txt

Generate diffs with <30> lines of context instead of the usual three

Push a New Branch To GItlab

git add .

git commit -m “phoenix integration test”

git checkout -b PhoenixIntegrationTest

git push origin -u HEAD

(git branch -d PhoenixIntegrationTest ---- to delete the branch)

Merge with Master Branch

git checkout workBranch

git merge master

(solve the conflict)

git commit 

git merge -s recursive -X theirs BranchName

(always use their codes when having conflicts)

Push to the Master Branch

Merge current branch with master branch first. Do build the latest project using Jenkins to ensure there is no failure or warning messages. Then push the branch to gitlab. Go to gitlab branch page and click the ‘compare’ button. If this is successful, send a merge request. Then tick the ‘delete source batch’ and accept the merge request.

Track a Branch Pushed to a Project in Gitlab

git checkout master

git pull

git checkout --track origin/branchtotrack

---or---

git fetch -v

git checkout DashControllerModule-0.893.6

Reset Staged Files

git reset         -delete all the staged files after ‘git add’ but before ‘git commit’, won’t delete the actual work

Squash Merge Branch

git merge --squash branchName

git commit -m ‘xxxx’

merge with branchName and put all commits into a new commit

Create and Push Remote Tags

git tag ‘tag’

git push origin <tagname>

Check the tag of the current commit

git tag -l --points-at HEAD

Delete Remote Tags

git tag -d 'dashz_tag'

git push origin :refs/tags/dashz_tag

Delete Local Branch

git branch -d the_local_branch

Delete Remote Branch

git push origin :the_remote_branch

Delete Existing Commit

git reset --hard HEAD~1

If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force

Diff a File in Master and branch

git diff master:COMMON/rainbow_base_logic_scriptlet_vector1.py -- rainbow_base_logic_scriptlet_vector1.py

Squash Commits

git rebase -i HEAD~4    -rebase the last four commits

Then git push --force origin branchName

Show all conflict files after bad merge

git diff --name-only --diff-filter=U

 

Merge codes and specify how to resolve conflicts

git merge -s recursive -X theirs feature

Revert t previous commit back to staging area

git reset --soft HEAD~1

原文地址:https://www.cnblogs.com/codingforum/p/5833926.html