| git init |
Initialises a git repo. The starting step |
| git add file1 file2 |
Add a file to git. seperate file by adding spaces |
| git commit -m “Commit Message” |
Commit a file to git. |
| git commit -a -m “Commit Text” |
Add and commit at the same time |
| git commit --amend |
Make changes to the last commit. Fix the typo or added new files to the last commit |
| git log —oneline |
View oneline logs of each commit with smaller hash |
| git branch |
show you all the branches |
| git branch branch1 |
create a branch name but not switches to that branch |
| git switch branch1 / git checkout branch1 |
switch to the new branch |
| git switch -c branchname / git checkout -b branchname |
creates a new branch and switches to that branch automatically |
| git branch -d branchname |
deletes a branch only if the changes are merged. You cannot delete a bramch that youre currently on. |
| git branch -D branchname |
force delete the branch even if its not merged with master branch |
| git branch -M NewName |
First swtich to that branch and perform rename a branch |
| git branch -v |
shows you all the branches with their last commits |
| git merge <branchname> |
Merges the changes of <branchname> onto master if you are on master. |
| git diff |
This will compare the changes that you have done while working(before using add) —> unstaged |
| git diff —staged or git diff —cached |
Shows staged changes |
| git diff Head |
shows the changes since the last commit(staged and unstaged) |
| git diff Head [File Name] |
shows the changes of a particular file since the last commit eg : index.html |
| git diff branch1..branch2 |
Lists all the changes between two branches |
| git diff branch1..branch2 [FileName] |
shows the change in particular file of two branches |
| git diff commit1..commit2 |
compare two commits |
| git stash / git stash save |
stash changes that are not yet ready to commit |
| git stash pop |
remove the most recent stashed changes in you stash and re apply them to your working copy. |
| git stash apply |
apply whatever is stashed away, without removing from the stash. |
| git stash clear |
delete all the stashes from the list. |
| git checkout [commit hash] |
jump into that timeline. . and you can git switch master to reattach the head. |
| git checkout Head <File> or git checkout — <FIle> (Head always refers to the latest commit). |
Discarding Changes: Basically go to back to the previous commit |
| git restore <File> |
undo the changes in the current working file. an alternative to git checkout Head <File>. |
| git restore —staged <File> |
remove the file from staging area. |
| git reset <commit-hash> |
reset the repo back to a specific commit. It doesnot remove the changes. (ger rid of some commits) |
| git reset —hard <commit> |
undo both commits and the actual changes in you files. |
| git revert <commit-hash> |
similar to reset but creates a brand new commit and then undos the changes from a commit, which means the history is saved |
| git remote and git remote -v |
lists the remote |
| git remote add <name> <url> |
adds a new remote (git remote add origin https;//github.com/balu/project1) |
| git push <remote> <branch> |
push the work onto github. (git push origin master) |
| git push <remote> <local branch> : <remote-branch> |
Push In Detail |
| git push -u origin master |
setup an upstream so that you can use git push instead of git push origin main |
| git branch -r |
view the remote branches our local repository know about. |
| git checkout origin/main |
See the file at the time of origin/master . goes to detached head. use git switch master to come back. |
| git fetch <remote> / git fetch <remote> <branch> |
fetch the remote changes from Github repo and bring it to our local repo.(not into working directory). git fetch origin master. Use git checkout origin/main to see the changes from github. |
| git pull <remote> <branch> |
Gets changes from remote branch(es) Updates the current branch with the new changes, merging them in. |