Git Cheatsheet
Git Cheatsheet
git init
See Start a new git repo for detailed instructions
Create a new local repo on the command line
$ cd /path/to/repo && echo "# repo_name" >> README.md
$ git init # create .git directory to track files
$ git add README.md
$ git commit -m "first commit"
$ git branch -M main # rename branch name to main, default is master
$ git remote add origin <REMOTE_URL> # example url: git@github.com:username/new_repo
$ git push -u origin main
Add local repo to Github
First create a new repo with repo_name
in the Github manually. Then push the local repo the remote.
$ git remote add origin <REMOTE_URL> # remote url should be git@github.com:username/new_repo
$ git push -u origin main # branch name should be specified
git clone
Command line
$ git clone git@github.com:username/repo_name # clone using SSH
$ git clone https://github.com/ysername/repo_name # clone using URL
Note: By default, git clones the master
or main
branch.
Clone a specific branch in the first place
$ git clone -b <branchname> <remote-repo-url>
git branch
Show all branches:
$ git branch # show all local branches
$ git branch -a # show all local and remote branches
Example of showing all branches:
git branch -a
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
remotes/origin/test-dev
Show detail of a specific branch:
$ git branch -vv # show details for all branch
$ git branch -vv <branchname> # show details for <branchname>
Switch to an existing local branch:
$ git checkout <existing_branch> # switch to <existing_branch>
We can also switch to remote branches
$ git checkout origin/main # You are in 'detached HEAD' state.
Add a new local branch (with the same name) to track an existing remote branch
$ git checkout <branchname> # create a new local branch (e.g., test-dev) to track the remote branch
Branch 'test-dev' set up to track remote branch 'test-dev' from 'origin'.
Switched to a new branch 'test-dev'
Create a new local branch:
$ git branch <branchname> # create a branch called <branchname>
$ git checkout <branchname> # switch to <branchname> branch
$ git checkout -b <branchname> # equivalent to the above two commands
Note: git branch <branchname>
does not switch to <branchname>
automatically.
Delete, rename, and copy local branch:
$ git branch -d <branchname> # “safe” operation, prevents from deleting the branch if it has unmerged changes
$ git branch -D <branchname> # deleting the branch even if it has unmerged changes
$ git branch -m [<oldbranch>] <newbranch> # <oldbranch> is optional if rename current branch
$ git branch -M [<oldbranch>] <newbranch> # If <newbranch> exists, -M must be used to force the rename to happen
$ git branch -c [<oldbranch>] <newbranch> # <oldbranch> is optional if copy current branch. -C similar to -M
Push a new local branch to remote or reset the upstream branch:
$ git push origin -u <upstream> # Branch 'local' set up to track remote branch '<upstream>' from 'origin'
Note: Run the command when pushing a local branch to the remote for the first time.
Note: <upstream>
name can be different from the local branch name. A local branch can be used to track any remote branch as long as the remote branch name is given. It is recommended to set the local branch name the same as the remote branch.
Delete a remote branch:
git push origin --delete <oldname>
Track local branch: (in contrast with track remote branches)
$ git branch -u <newlocal> # Branch 'local' set up to track local branch '<newlocal>'
git remote
Check current remote repo URL
$ git remote -v # output fetch and push url
Set new URL for remote
First check existing remotes and their names. Commonly there is only one remote origin
.
$ git remote set-url origin https://github.com/user/repo2.git # use origin as an example