Here's a list of basic Git commands that I use daily for dev.
Starting with the basics, this command makes a copy of the remote to the local machine.
git clone <repo:url>
(The repo looks like: https://github.com/ajayns/github-cmds)
This set of commands is used to make changes, save them and push them to the remote, better explained step by step:
First, add the files to git
git add -A
(.
will add all files and folders, so file names in its place can be used to add files specifically)
Commit the changes
git commit -m 'Add commit message here'
(A commit message is a brief account of changes made, like 'Minor fixes', 'Added login component')
Add the remote to the Git repo
git remote add origin <repo:url>
Push the changes to the remote
git push -u origin master
(origin
can be replaced with other remotes depending where you want to push to. master
refers the main branch, it can be changed to whichever branch you'd like to push)
Pushing commits needn't be done everytime you make a commit, instead, multiple commits can be push together from local to remote.
Create a new branch from current one
git branch <branch-name>
Switch to a branch
git checkout <branch-name>
Create new branch and switch to it
git checkout -b <branch-name>
Delete a branch
git branch -d <branch-name>
Merge a branch to master,
git checkout master
(making sure you are in the master branch)
git merge <branch-name>
Fetching a branch from remote and track it locally by adding new branch to the local repo
git fetch <remote> <branch-name>:<local-branch-name>
Clone the remote repo to local system and the following setups will bring the local and remote repos up to date with the source repo.
Add the source remote (where you cloned from) to git, calling it upstream
git remote add upstream <repo:url>
Fetch all branches from upstream
git fetch upstream
Switch to master branch
git checkout master
Rewrite your master branch so that any commits of yours that aren't already in upstream/master are replayed on top of that other branch:
git rebase upstream/master
(These same steps can be done for branches other than master also)
Finally, push the repo to your forked remote to make it up to date
git push -f origin master
(Use the -f
only when first rebase)
ALT:
Add the source remote (where you cloned from) to git, calling it upstream
git remote add upstream <repo:url>
Fetch all branches from upstream
git fetch upstream
Merge by pull
git pull upstream master
Squashing previous 'n'commits into one new commit
// Assuming you want to squash previous 2 commits
// HEAD~n specifies number of commits to squash
git reset --soft HEAD~2 && git commit -m 'New commit message here'
To squash in interactive rebase mode
git rebase -i HEAD~2
Follow the steps after this screen, to select and merge from terminal itself.
Discards all changes made in working directory
git clean -df
git checkout .
(clean
is used to clear all changes made to tracked files while checkout .
deletes all untracked files)
Alternatively, to save all changes made to working directory away and revert it to match the HEAD (last) commit
git stash -u
Locally delete branch:
git branch -D <branch-name>
Delete remote branch:
git push <remote_name> --delete <branch_name>
View all of the remotes
git remote -v
Add a new remote
git remote add <remote-name> <remote-url>
Changing an exising remote's URL
git remote set-url <remote-name> <new-remote-url>
To undo 1 commit
git reset HEAD~1
To change the commit message of last commit, which is not pushed
git commit --amend