## Reverting a File to a Previous Commit - see all commits made to App.js. ```bash $ git log -- oneline src/App.js ``` - Use the SHA value to to revert to the desired version ```bash $ git checkout 55a1dff -- src/App.js ``` ## When I need to rename a branch - Local branch `git branch -m ` ## When I need to checkout a branch I might not know the exact name of the branch, but I do know it contains some word - `git branch --r | grep -i ` Maybe I don't have all the branches in sync with the remote repo, so I need to fetch them: - `git fetch origin` Checkout remote branch - `git checkout -b origin/` List all branches in your working directory - `git branch` Switch to another branch - `git checkout ` Create a new local branch - `git checkout -b ` Push your branch to the remote - `git push -u origin ` (u = set_upstream) ## Remove all files from the working area (untracked files) See which files would be deleted - `git clean -n` Delete the files - `git clean -fd` ## Remove changes you made to a file if you haven't staged the changes yet - `git checkout -- ` ## Unstage a file that is not tracked - `git rm --cached ` ## Unstage a file that is tracked - `git reset HEAD ` # Useful git commands ## Basics `git init` - initiate a repository **`git status`** - get the status of repository / staging area / working directory **`git add -u`** - adds only changed files to the staging area -> untracked files will be ignored `git add .` - adds all files (tracked or not) to the staging area `git diff` - shows differences between the working directory and the repository `git diff --staged` - shows differences between the staging area and the repository **`git commit -m "message here"`** - commit your changes to the repository `git rm ` - it will remove the file and add the change to the staging area `git log` - shows the list of commits made `git log -n 3` - shows only the last 3 commits `git log --oneline` - every commit is on one line `git log --graph --oneline --all --decorate` - shows a nice graph of the commits ## Undoing changes `git checkout -- ` - undo changes you made to this file in the working directory. This brings back the file from the repository `git reset HEAD ` - unstage file `git commit --amend -m "message here"` - amend the last commit; Note: you can amend only the last commit `git revert commit-sha-code` - it will make all the opposite of what that commit did and register these changes as a new commit `git reset commit-sha-code` - it sets the HEAD to the commit given in the command. * `--soft` - for just changing the HEAD pointer, but keeping the staging index and the working directory unchanged * `--mixed` - changes the HEAD pointer and resets the staging area. Working directory stays unchanged * `--hard` - changes the HEAD pointer, resets staging area and working area. If I add a new commit now, the commits after the reset will be lost. `git clean -n` - shows which files would be deleted `git clean -fd` - removes untracked files `git checkout -- .` - unstage all modified files ` git submodule update --init --recursive` - discard changes in submodules ## Branches `git branch` - show the existing Branches `git branch -r` - show the branches on the remote repository `git checkout -b branchName` - create and checkout a branch `git diff branch1..branch2` - get differences between branch1 and branch2 `git branch -d branchName` - deletes a branch To merge branch1 into master `git checkout master` - switch to master (the branch that things are being merged into - the receiving branch) `git merge branch1`- merge branch1 into master `git merge --abort` - abort while in the merge To set an existing local branch track a remote branch `git branch -u origin/` Create a local branch and set it to track a remote branch `git branch -t origin/` To move one commit from one branch to another - when the bad commit is the last one made `git checkout ` `git cherry-pick ` - this will move the commit to the branch you are currently on `git checkout ` `git reset --hard HEAD^` - this will delete the last commit which is the commit you already moved ## Remote repositories `git remote add origin https://github.com/etc` - adds a remote found at that url and with the name origin to your local repository `git remote rm origin` - it removes the remote repository with the name origin `git push -u origin master` - push the changes in the master branch up to the origin remote repository `git remote set-url origin git@devtopia.esri.com:WebGIS/webscene-spec.git` - change a remote's url `git remote -v` - list all the remotes ## Workflow when collaborating * when working on master: before commiting your changes always pull so that he can merge using fast-forward; if you commit your changes before you pull, git will merge the 2 master branches (remote and local) using the recursive strategy * when working on your own branch and you need to put your branch up to date with the master branch, you can do: `git rebase master` - while being on the branch you need to rebase, this will add all commits from master to your branch before the commits in your branch * to mention an issue from another repo in a commit: WebGis/arcgis-js-api#1234