This post list commonly used git commands.
Git remote command lets you create, view and delete connections to other repositories.
git remote show <remote>
Show the details of
git remote -v
List the remote connections you have to other repositories.
git remote add <name> <url>
Create a new connection to a remote repository.
git remote rm <name>
Remove a remote repository.
git remote rename <oldname> <newname>
Change the name of a remote repository
git fetch <remote>
Fetch all of the branches from the repository.
git fetch <remote> <branch>
Fetch a specific
git push <remote> <branch> git push <remote> --all
Push the specified branch to
<remote>, along with all of the necessary commits and internal objects. Or, push
all branches with
git push <remote> --force
Forciably push local branch to remote branch. Local branch commits will overrule the remote commits if they have conflicts.
refs and refspecs
A ref is an indirect way of referring to a commit. You can think of it as a user-friendly alias for a commit hash.
A refspec maps a branch in the local repository to a branch in a remote repository. A refspec is specified as
[+]<src>:<dst>.The optional + sign is for forcing the remote repository to perform a non-fast-forward update.
git push origin master:refs/heads/qa-master
This command pushes the master branch to the origin remote repo like an ordinary git push, but it uses qa-master as the name for the branch in the origin repo.
By default, git fetch fetches all of the branches in the remote repository. The reason for this is the following section of the
[remote "origin"] url = https://[email protected]:mary/example-repo.git fetch = +refs/heads/*:refs/remotes/origin/*
To fetch only the master branch, change the fetch line to match the following:
[remote "origin"] url = https://[email protected]:mary/example-repo.git fetch = +refs/heads/master:refs/remotes/origin/master
You can also use refspecs for deleting remote branches.
git push origin :some-feature-branch
When you check out a previous commit, HEAD no longer points to a branch – it points directly to a commit.
-- sign is a way to tell Git to treat what follows checkout as a file and not as a branch. Suppose that you had both a file and a branch called stuff. Then the following command would seem ambiguous:
git checkout stuff
because it is not clear whether you are asking to checkout a file or a branch. By using
-- you explicitly tell Git to checkout a file by that name/path. A file level checkout will change the file’s contents to those of the specific commit.
Git revert prevents git repository from losing history.
git revert undoes a single commit -— it does not “revert” back to the previous state of a project by removing all subsequent commits.
git revert is able to target an individual commit at an arbitrary point in the history, whereas
git reset can only work backwards from the current commit. For example, if you wanted to undo an old commit with
git reset, you would have to remove all of the commits that occurred after the target commit, remove it, then re-commit all of the subsequent commits.
You should never reset to change history that have been shared with other developers.
git reset --soft (delete head) --mixed (delete index and head) . This is the default behavior. --hard (delete head, index and work directory)
git reset --hard HEAD
wipe out all local changes to match last commit