How to clone a repository from the git server
git clone repo_url
How to remove files from git
git rm file.txt git commit -m "remove file.txt" git push
*note: git rm deletes the file both from the local directory and the git repository.
How to create a remote branch
git branch mybranch git checkout mybranch git push --set-upstream origin mybranch
How to merge a git branch into master
git checkout master git pull origin master git merge mybranch git commit git push origin master
How to merge master into a git branch
git checkout mybranch git rebase master
git rebase makes it as if the changes on the feature branch were made on top of the changes on the master branch, and it makes the version graph simpler.
git rebase is only suitable when the branch hasn’t been distributed, or there will be confusion and extra work downstream. [reference]
How to check the difference between two branches
git diff --name-status master..mybranch
How to list all (local and remote) branches
git branch -a
How to list all remote branches only
git branch -r
How to resolve merge conflicts in a file
- Resolve conflicts in a file manually: Look for markers <<<<<<<, ======= and >>>>>> in the file and make the edits. When done, run the following command…
git add file_with_conflicts_resolved
- Use the version from the source branch and overwrite your changes…
git checkout --theirs file_with_conflicts git add file_with_conflicts
- Use your version and reject the changes in the source branch…
git checkout --ours file_with_conflicts git add file_with_conflicts
How to abort/undo a merge
Before the changes are committed, the merge can be aborted/undone. After the abort the state goes back to the point before the “git merge” command was issued.
git merge --abort
How to change a branch to master
git checkout mybranch git merge --strategy=ours --no-commit master git commit git checkout master git merge mybranch
To undo changes made to the working copy:
git checkout .
To undo changes made to a specific file:
git checkout /path/file/
To revert changes made to the index (file that have been added):
To revert a change that have ben committed:
git revert ...
More info about undoing changes here