For those new to command line, ending up at the commit message screen (often when you forget to the add -m
flag to a commit) is confusing because pressing escape (or CTRL
+ C
) does not exit the screen, as the default editor for Git is VIM. Instead, press escape (if you've started attempting to type something) and type the following command:
:q
And press enter, and you'll return to where you were.
If you have a project on your computer and you just created an empty Git repository in GitHub, use these commands to upload everything to Git.
cd your-directory
git init
git remote add origin [email protected]:your-username/your-repo.git
git add .
git commit -am "Message"
git push -u origin master
The opposite of the above option - for example, if your repository exists in GitHub, and you're working on it in a different local computer. Run this command outside of where you want the new directory to appear (not within the directory you want it to appear).
git clone [email protected]:your-username/your-repo.git # using SSH
git clone https://github.com/your-username/your-repo.git # using HTTPS
Remove one cached file.
git rm -r —-cached file.txt
If you have some merge conflicts, or accidentally started to make a change to your local directory before pulling the changes from the master, here's how you can revert your local directory to what's on GitHub.
git fetch --all
git reset --hard origin/master
If you've been tracking a directory and later decide to ignore the whole directory, simply adding it to .gitignore
isn't enough. First you must add the directory to .gitignore, then run this command:
git rm -r --cached your-directory
Then push the changes.
Similar to above, but if you've added a .gitignore
with a lot of changes.
git rm -r --cached .
git add .
git commit -m "Message"
When you really want your local repository to override the remote.
git push -f origin master
git pull -f origin master
Make a new branch with their changes.
git checkout -b their-branch master
git pull their.git master
Play with the files and commit them.
git add files
git commit -m “Message"
git push origin master
Merge back into your branch.
git checkout master
git merge --no-ff <their-branch) (:wq!)
git push origin master
Put a :
in front to remove instead of update remotely.
git push origin :branch-name
Use --delete
or -D
for local.
git branch --delete branch-name
git checkout branch-name
git merge -s ours master
git checkout master
git merge branch-name
git branch | grep -v "master" | xargs git branch -D
More than one branch may be added to the grep. To remove all local branches except "master" and "develop":
git branch | grep -v "master\|develop" | xargs git branch -D
Fix the problem of git hooks claiming everything is "Up-to-date".
git push production master
git commit --allow-empty -m 'push to execute post-receive'
git push production master
Merge branches.
git checkout master
git pull origin master
git merge new-feature
git push origin master
git fetch --prune --all
git checkout other-branch
git fetch origin
git checkout --track origin/<remote_branch_name>
git pull -Xtheirs
git fetch --prune --all
git rebase origin/develop
git pull
git push
Put your changes away and switch to another branch
git stash
git checkout -b new-branch
git stash pop
git branch new-branch
git reset HEAD~1
git checkout <files>