Skip to content

Instantly share code, notes, and snippets.

@RalucaNicola
Last active November 21, 2022 20:24
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RalucaNicola/98c8a4e89ba901265f5536ec4bc2e419 to your computer and use it in GitHub Desktop.
Save RalucaNicola/98c8a4e89ba901265f5536ec4bc2e419 to your computer and use it in GitHub Desktop.
Common Github commands for work

All these commands can be followed along using this repository:

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 <pattern>

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 <branch_name> origin/<branch_name>

List all branches in your working directory

  • git branch

Switch to another branch

  • git checkout <branch_name>

Create a new local branch

  • git checkout -b <branch_name>

Push your branch to the remote

  • git push -u origin <branch_name> (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 -- <file>

Unstage a file that is not tracked

  • git rm --cached <file>

Unstage a file that is tracked

  • git reset HEAD <file>

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 <file> - 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 -- <file> - undo changes you made to this file in the working directory. This brings back the file from the repository

git reset HEAD <file> - 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/<branch-to-track>

Create a local branch and set it to track a remote branch

git branch -t <branch-name> origin/<branch-name>

To move one commit from one branch to another - when the bad commit is the last one made
git checkout <branch-to-move-the-commit-to>
git cherry-pick <commit id> - this will move the commit to the branch you are currently on
git checkout <original-branch>
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment