Git - sgml/signature GitHub Wiki
To see all branches, use fetch
with the all
and verbose
options:
git fetch --all --verbose
To see all branches by name, use branch
with the -r
option, pipe to grep
, and use sed
to strip the word origin
:
git branch -r | grep 'foo' | sed 's/origin\///'
To clone a specific branch, use `--single-branch --branch <branchname> <remoterepo>:
git clone --single-branch --branch foo [email protected]:foo-bar/baz.git
To sync a new repo, make a new folder and go to it:
<pre> mkdir foobarbaz cd foobarbaz Create the directories, initialize the local git repo, and copy the code locally, create a branch:
mkdir framework cd framework git init git clone ssh:foobarbaz.git git remote remove origin git remote add origin [email protected]:myname/foobar.git git branch --set-upstream-to=origin/master master git push --set-upstream origin master
To view remotes:
git remote -v show
To create a new branch, use git checkout -b:
git checkout -b myBranch
To link a new branch to a remote repo, use git branch --set-upstream-to:
git branch --set-upstream-to=origin/project/sprint-25-3-8to3-21 feature/INVPROD-2419
To list all remote branches, use git branch -r:
git branch -r
git branch -r | grep localhost
To switch branches, use git checkout with no options:
git checkout foo
To reset existing commits and create a new branch, stash your local env, create a branch from it, push the changes, switch to the current top-level branch, then pull in the latest changes:
git push git stash git stash branch local-changes git add * git push git checkout master git pull git checkout -b newbranch git checkout -p origin/local-changes
To check in, use the following process:
Add new files:
git add foo
Stash changed files:
git stash
Stash specific files:
git stash push [--] [<pathspec>...]
Pull new changes from the repo:
git pull
Show stashed file list:
git stash show
List stashed changes:
git stash list
Merge stashed changes:
git stash apply
Commit pushed changes:
git commit -m "foo bar baz"
Amend an unacceptable commit message:
git commit --amend
Undo an amended or other staged commit:
git reset 'HEAD@{1}' --soft
Revert the last pushed commit
git revert HEAD
Push new changes to the repo:
git push
Diff current remote to an older branch:
git diff FETCH_HEAD...feature/MAS-2782 --diff-filter=A
Diff last two revisions:
git diff 7194f404d5dccbb177bf4ea5aefa4d60081def31..452db42589004b3b2838933d6fcb6e046f980f28
One file copy from one commit to the local branch:
git show [commit hash] -- /foo/bar/baz.js > /foo/bar/baz.js
Remove a file
git rm -f ngtemplates.js
Reset the local branch HEAD to the remote branch HEAD
git reset git checkout -b feature/INVPROD-4781 git branch --set-upstream-to=origin/feature/INVPROD-4781 feature/INVPROD-4781 git reset --hard FETCH_HEAD
-
Checkout master
-
Create a branch from it called misc/foo
-
Cherry pick from develop For example:
git push # sync the branch you're working on to its remote git pull # pull in the merged changes git fetch git checkout master # change branches to the one you want to modify git cherry-pick 533c2093200 #merge; Run git status, resolve conflicts, Run git add, then commit and push
If you pull all cherry pick files this way, you can create a yaml file and parse them using tac
, awk
, and xargs
:
tac commits.yaml | awk '// {print $2}' | xargs git cherry-pick
git pull origin develop
git checkout -b myDevIntgrMerge #new branch
git checkout -p origin/foo -- ./test #pick branch and subdirectories
If you are several weeks or months behind on a branch, use the following process: * For each conflict in each file, copy the HEAD changes and paste them to the end of the file * For each conflict at the end of the file, compare the fragments to the source file in the branch * Piece together the conflict hunks at the bottom of the file, then resolve the conflicts one by one * Run a linter to make sure there are no syntax errors * Merge the newly resolved files
When you get into a failed merge state, the index/stage splits into three. The first index is the abyss. The second index is your version of things. The third is the remote version of things. From here, you can either git checkout file --ours or git checkout file --theirs. This will accept either your file or their file into the stage, overriding the merge conflict in that file. git commit and that pesky merge conflict is dead.
git checkout origin/dev-intgr --ours -- foo.bar
git checkout origin/feature/baseline --ours -- ApplicationMockData.js
Auto reset and merge to rollback to an unmerged commit
git pull origin baseline
git reset HEAD 12345
git checkout -b rebaseline
git merge -s ours origin/feature/baseline
Rollback a repo to a merged commit
git checkout master git checkout -b rollback git checkout -p rev_to_rollback_to
Rollback a file to a merged commit
git checkout master git checkout -b rollback git checkout -p rev_to_rollback_to ./index.html
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]?
Using the following key:
y - apply this hunk to index and worktree n - do not apply this hunk to index and worktree q - quit; do not apply this hunk or any of the remaining ones a - apply this hunk and all later hunks in the file d - do not apply this hunk or any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help
Then push to the remote using:
git push --set-upstream origin myDevIntgrMerge #push merge
Merge to stable from unstable without accepting any commits
git merge origin/feature/INVPROD-2898-release -s ours
Merge to unstable from stable without auto resolving conflicts
git merge origin/integration -s recursive -X ours
Test for Bitbucket Conflicts Locally
You need to update your local master branch. So do the following steps :
git checkout master git pull origin master
Resolve the conflicts here, then run:
git add * git stash git checkout << your branch >>
To pull in your local changes, run git stash apply
git merge master
Resolve the conflicts again
git add * git commit git push
For starters, git merge -s ours xyz is not the same as git merge -X ours xyz. The first uses merge strategy called “ours”, and the second uses default ‘recursive’ merge strategy, but with “ours” option. Creating two entities named “ours” that do similar, but subtly different things is the epitome of bad interface design.
The “-s” variant creates a commit that merges current branch and “xyz”, but ignores all changes from xyz, so the resulting content would be identical to the current branch. This is useful for getting rid of unwanted branches when force-pushes and deletes are disabled. The “-X” variant only ignores changes from xyz that cause a conflict. Changes from xyz that do not cause a conflict will be included in the end result.
To fix an invalid commit message(no iTrack, no whitespace, etc), do the following:
git commit --amend
To clean mistaken deletes or checkins, do the following:
git reset --hard HEAD
Then clean the untracked files and directories with:
git clean -df
-
git branch -d example
-
Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
-
Reset the upstream branch for the new-name local branch.
git push origin -u new-name
Create a netrc file:
Linux: vi ~/.netrc Windows: New-Item -Name _netrc -ItemType File -Path $env:userprofile Set the permissions for your eyes only:
chmod 0600 ~/.netrc Then add the bitbucket URL and your username and password:
machine bitbucket.etrade.com login myusername password mypassword
-
bugfix/: bug fixes
-
hotfix/: hot fixes for production
-
release/: code for new feature releases
-
chore/: documentation, copy, and image
-
experiment/: demos
-
wip/: drafts
-
improvement/: refactoring
[Git User Manual](https://www.kernel.org/pub/software/scm/git/docs/user-manual.html)