The following content contains a reference document that demonstrates how to execute the most common Git commands using the command-line.
If you have a project on your local computer and you created an new Git repository on GitHub, you can use the following commands to quickly upload your project to your new Git repository on GitHub:
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
To download a repository from a Git repository to a local directory, run the following command:
git clone https://github.com/your-username/your-repo.git # using HTTPS
To remove one cached file, run this command:
git rm -r —-cached file.txt
If you have to resolve any merge conflicts in your project's local directory, you can use the following commands to override all local changes to your project; as such, this subsequently allows you to pull from the master from your Git repository.
git fetch --all
git reset --hard origin/master
To allow your local directory to override the remote Git repository, you can run these two commands:
git push -f origin master
git pull -f origin master
- To make a new branch with their changes.
git checkout -b their-branch master
git pull their.git master
- Create a file from your local directory and commit those changes to your Git repository.
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
To remove a branch from a local directory or remote repository, use the following commands:
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
To allow empty commits, run the following commands:
git push production master
git commit --allow-empty -m 'push to execute post-receive'
git push production master
git checkout master
git pull origin master
git merge new-feature
git push origin master
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
Use stashing to save and archive local changes from one branch so that you can switch to another branch.
git stash
git checkout -b new-branch
git stash pop
git subtree push --prefix dist origin gh-pages