= config git =
== show current configuration ==
git config --list
,~/.gitconfig
file..git/config
file.= Branch OP =
== create remote branch ==
First, you create your branch locally:
git checkout -b <branch-name> # Create a new branch and check it out
The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can just do:
git push <remote-name> <branch-name>
Where <remote-name>
is typically origin
, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.
Note however that formally, the format is:
git push <remote-name> <local-branch-name>:<remote-branch-name>
But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name>
(with the colon), or the remote branch will be deleted!
So that a subsequent git pull
will know what to do, you might instead want to use:
git push --set-upstream <remote-name> <local-branch-name>
As described below, the --set-upstream
option sets up an upstream branch:
== delete branch ==
=== local ===
Delete a Local GIT branch
To delete the local GIT branch we can try one of the following commands:
git branch -d branch_name
git branch -D branch_name
The -D
option stands for --delete --force
, which deletes the branch regardless of its push and merge status, so be careful using this one!
=== remote ===
Delete a remote GIT branch
To delete a remote branch you can use the following command:
git push <remote_name> --delete <branch_name>
== rename branch ==
1. Rename your local branch.
If you are on the branch you want to rename:
If you are on a different branch:
2. Delete the old-name remote branch and push the new-name local branch.
3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:
1
1
1
git branch -m new-name
git branch -m old-name new-name
git push origin :old-name new-name
1
git push origin -u new-name
= File OP =
== rm /reset ==
git rm --cached <file>
makes git stop tracking the file completely (leaving it in the filesystem, unlike plain git rm
*)git reset HEAD <file>
unstages any modifications made to the file since the last commit (but doesn't revert them in the filesystem, contrary to what the command name might suggest**). The file remains under revision control.== overwrite local file(s) ==
git checkout <path-to-file>
== ==
PR / Merge OPs
= Interactive merge =
git merge --no-commit --no-ff <branch-to-merge>
… which stages everything, so to unstage all:
git reset HEAD
Then begin adding the pieces you want:
git add --interactive
or
git add -u // to add all `unstaged`
= roll back =
git reflog
command to identify the last-known-good state of your repogit reset --hard <commit>
to revert back to itgit push --force
to reset the remote repository back to that state== sign off ==
run below after merge is done (for signing off):
git commit --amend --no-edit -s
=create-a-GitHub-pull-request-with-a-specific-commits =
1. First, you need to create a branch with the latest changes (that is the same with the upstream remote branch)
git fetch --all git checkout -b new-branch-name upstream/master ?
git checkout -b newbranch && git branch -u upstream/master
2.Next, you would need to cherry-pick the changes that you would like to include in the pull request.
git reflog to show the hash info you want to choose
git cherry-pick a51afa6 git cherry-pick 07f39f7
...
3.Now, all you need to do is to push your changes to your GitHub repository
git push -u origin new-branch-name ?
git push <remote-name> <local-branch-name>:<remote-branch-name>
= Consider cleaning up the .git folder to reduce the large repo size =
To see the 10 biggest files, run this from the root directory:
git verify-pack -v .git/objects/pack/pack-7b03cc896f31b2441f3a791ef760bd28495697e6.idx \ | sort -k 3 -n \ | tail -10
To see what each file is, run this:
git rev-list --objects --all | grep [first few chars of the sha1 from previous output]