Last active
April 3, 2019 15:50
-
-
Save injune1123/6d9f91e0ddbfce7a4dc35d07024048b6 to your computer and use it in GitHub Desktop.
My Frequently used GitHub commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Learning resources: | |
https://learngitbranching.js.org/ | |
I stashed twice, what shall I do????!!!! | |
https://www.atlassian.com/git/tutorials/saving-changes/git-stash | |
$git stash list | |
$git stash apply stash@{n} | |
Switching remote URLs | |
List your existing remotes in order to get the name of the remote you want to change. | |
$ git remote -v | |
origin [email protected]:USERNAME/REPOSITORY.git (fetch) | |
origin [email protected]:USERNAME/REPOSITORY.git (push) | |
Change your remote's URL | |
$git remote set-url origin https://github.com/USERNAME/REPOSITORY.git | |
Verify that the remote URL has changed. | |
$git remote -v | |
# Verify new remote URL | |
origin https://github.com/USERNAME/REPOSITORY.git (fetch) | |
origin https://github.com/USERNAME/REPOSITORY.git (push) | |
//Add git autocompletion script to iterm | |
https://apple.stackexchange.com/questions/55875/git-auto-complete-for-branches-at-the-command-line | |
ok, so I needed the git autocompletion script. | |
I got that from this url: | |
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash | |
No need to worry about what directory you're in when you run this as your home directory(~) is used with the target. | |
Then I added to my ~/.bash_profile file the following 'execute if it exists' code: | |
if [ -f ~/.git-completion.bash ]; then | |
. ~/.git-completion.bash | |
fi | |
// Check where do the settings in my Git configuration come from | |
git config --list --show-origin | |
// fetch all local changes | |
git fetch --all | |
// bring local master up to date as upstream master | |
git merge upstream master | |
git push origin master | |
git reset --hard upstream/master | |
//set up stream | |
git remote -v | |
// get other's work | |
git checkout -b temp-branch | |
git fetch --all | |
git branch --all | |
git rebase #{remote-name}/#{branch-name}, e.g. git rebase mike/my-nice-branch | |
git cherry-pick #{a-git-sha} | |
// change a local branch's name, rename a local branch | |
git branch -m #{fancy-new-name} | |
// git verbose info of a branch | |
git branch -v | |
// git even more verbose info of a branch | |
git branch -vv | |
// | |
git branch --set-upstream-to #{remote-name}/#{branch-name}, e.g. git branch --set-upstream-to origin/my-awesome-feature-branch | |
// get other people's change onto a new branch | |
git checkout -b a-new-branch master | |
git pull https://github.com/#{user-name}/#{repo-name}.git #{branch-name} | |
//undo a git add | |
git reset #{file-path} | |
//undo a git commit that is already pushed to github | |
// situtation on my forked repo page This branch is 1 commit ahead of <the original repo>:master. | |
git reset <previous label or sha1> | |
git push -f <remote-name> <branch-name> | |
//remove untracked files (This can be dangerous!!! Your .env files might be gone!!!! ) | |
To remove directories, run git clean -f -d or git clean -fd. | |
To remove ignored files, run git clean -f -X or git clean -fX. | |
To remove ignored and non-ignored files, run git clean -f -x or git clean -fx. | |
// add a remote. Use case, when more than 1 people is forking a branch, and you want to test other's changes | |
git remote add ${the-name-of-the-remote-to-track-others-work} ${the-CLONE-WITH-HTTPS-LINK-ON-GITHUB} | |
git fetch --all // get all the branches of the other people | |
git branch --all // take a look at all the branches | |
Removing a remote | |
Use the git remote rm command to remove a remote URL from your repository. | |
The git remote rm command takes one argument: | |
A remote name, for example, destination | |
Example | |
These examples assume you're cloning using HTTPS, which is recommended. | |
git remote -v | |
# View current remotes | |
origin https://github.com/OWNER/REPOSITORY.git (fetch) | |
origin https://github.com/OWNER/REPOSITORY.git (push) | |
destination https://github.com/FORKER/REPOSITORY.git (fetch) | |
destination https://github.com/FORKER/REPOSITORY.git (push) | |
git remote rm destination | |
# Remove remote | |
git remote -v | |
# Verify it's gone | |
origin https://github.com/OWNER/REPOSITORY.git (fetch) | |
origin https://github.com/OWNER/REPOSITORY.git (push) | |
// resolve a binary file conflict with Git | |
//warning: Cannot merge binary files: xxx/public/img/favicon.ico (HEAD vs. master) | |
//Auto-merging xxx/public/img/favicon.ico | |
//CONFLICT (add/add): Merge conflict in xxx/public/img/favicon.ico | |
(Reference link https://lostechies.com/joshuaflanagan/2010/01/29/how-to-resolve-a-binary-file-conflict-with-git/) | |
1) make a decision: keep the version of the file in your current branch, or the version in the other branch. | |
1.a) Resolve using mine | |
The file in your working copy is still the copy from your current branch – in other words, it was not modified by the merge attempt. To resolve the conflict and keep this file: | |
$git add xxx/public/img/favicon.ico | |
$git commit –m “My commit message for the merge” | |
1.b) Resolve using theirs | |
$git checkout otherbranch xxx/public/img/favicon.ico | |
$git add xxx/public/img/favicon.ico | |
$git commit –m “My commit message for the merge” | |
Add a file to the last commit in git? | |
$git add the_left_out_file | |
$git commit --amend --no-edit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment