Skip to content

Instantly share code, notes, and snippets.

@miranda-zhang
Last active December 6, 2019 03:33
Show Gist options
  • Save miranda-zhang/4fd587c2a793e85667c1938eda782217 to your computer and use it in GitHub Desktop.
Save miranda-zhang/4fd587c2a793e85667c1938eda782217 to your computer and use it in GitHub Desktop.
git commands

Git basic

https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

Test ssh connection:

ssh -T [email protected]
ssh -T [email protected]

See help doc:

$ git <command> --help

Git global setup

git config --global user.name "Qian Zhang"
git config --global user.email "[email protected]"

Create a new repository

git clone https://gitlab.cecs.anu.edu.au/u4033585/comp2100labsolution2018.git
cd comp2100labsolution2018
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

Existing folder

cd existing_folder
git init
git remote add origin https://gitlab.cecs.anu.edu.au/u4033585/comp2100labsolution2018.git
git add .
git commit -m "Initial commit"
git push -u origin master

Existing Git repository

cd existing_repo
git remote rename origin old-origin
git remote add origin https://gitlab.cecs.anu.edu.au/u4033585/comp2100labsolution2018.git
git push -u origin --all
git push -u origin --tags

Add all updated files to git directory

$ git add -u .

It will stage the modified and deleted files. Read more here.

To undo git add . use git reset (no dot).

Remove a file named filename.txt from the current index without changing anything else.

$ git reset filename.txt

Remember SSH passphrase (for current terminal session):

$ eval `ssh-agent -s`
$ ssh-add [-k]

Download/Upload changes

$ git pull/push origin <branch>

Current status

$ git status

Switching Branches

$ git checkout <branch>   
$ git checkout --track origin/INS-5708 

Discard local changes:

$ git checkout -- .    

Change from http to ssh

$ git remote set-url origin [email protected]:DataAnalytics-Bench/CodeBench.git

Add remote repo

$ git remote -v

-v verbose

$ git remote add <name> <git repo url>

Commit

$ git commit -am "here goes a message"

Means almost the same thing as git add -u + git commit There's a subtle difference if you're not at the root directory of your repository. git add -u stages updates to files in the current directory and below, it's equivalent to git add -u . whereas git commit -a stages and commits changes to all tracked files.

Amend

Amend last commit message

git commit --amend

Vim:

Add ! to overwrite.

:exit to exit vim.

See what has changed in a file before committing to git

Show differences between index and working tree

that is, changes you haven't staged to commit

git diff [filename]

show differences between current commit and index

that is, what you're about to commit

git diff --cached [filename]

--stageddoes exactly the same thing, use what you like

show differences between current commit and working tree

git diff HEAD [filename]

It'll work recursively on directories, and if no paths are given, it shows all changes.

Read more here.

Remove

git rm file_from_disk
git rm --cached only_from_git_but_leave_on_disk

Branch

https://www.atlassian.com/git/tutorials/using-branches

git branch

List all of the branches in your repository. This is synonymous with git branch --list.

git branch <branch>

Create a new branch called <branch>. This does not check out the new branch.

git branch -d <branch>

-d Delete the specified branch. This is a “safe” operation in that Git prevents you from deleting the branch if it has unmerged changes.

git branch -D <branch>

-D Force delete the specified branch, even if it has unmerged changes. This is the command to use if you want to permanently throw away all of the commits associated with a particular line of development.

git branch -m <branch>

Rename the current branch to <branch>.

git branch -a

List all branches.

git branch -r

List remote branches.

Delete remote branch:

git push origin --delete branch-name

Upstream

$ git remote add upstream [url]

"origin" is a clone of your forked repo, from which you push and pull. "Upstream" is a name for the main repo, from where you pull and keep a clone of your fork updated, but you don't have push access to it.

$ git pull upstream master

Fetch

git fetch <remote> <rbranch>:<lbranch> 
git checkout <lbranch>

where <rbranch> is the remote branch or source ref and <lbranch> is the as yet non-existent local branch. https://stackoverflow.com/a/16095458/646732

Merge

$ git merge <branch name>

Merge <branch name> into current.

Submitting a Pull Request

For example on GitHub:

Add 'upstream' repo to list of remotes

git remote add upstream https://github.com/UPSTREAM-USER/ORIGINAL-PROJECT.git

Verify the new remote named 'upstream'

git remote -v

Switch to your local master branch

git checkout master

Fetch upstream and merge upstream master into your branch

git fetch upstream
git merge upstream/master

Once you've committed and pushed all of your changes, go to the page for your fork on GitHub, click the pull request button. If you need to make any adjustments to your pull request, just push the updates to GitHub. Your pull request will automatically track the changes and update. https://gist.github.com/Chaser324/ce0505fbed06b947d962

Log

Log in one line

$ git log --oneline

See log, filtered by author

$ git log --author='^(?!Adam|Jon).*$' --perl-regexp

https://stackoverflow.com/questions/4259996/how-can-i-view-a-git-log-of-just-one-users-commits

Reverse commit

Reset

git reset --soft HEAD~1
git reset --hard HEAD~1

--hard will change head, index and working directory.

--soft will change head only. No change to index, working directory.

So in other words if you want to undo your commit, --soft should be good enough. But after that you still have the changes from bad commit in your index and working directory. You can modify the files, fix them, add them to index and commit again.

With the --hard, you completely get a clean slate in your project. As if there hasn't been any change from the last commit. If you are sure this is what you want then move forward. But once you do this, you'll lose your last commit completely.

Checkout

git checkout <commit id> <file>

Checkout a single file from a specific commit. Sometimes we mess around with a file and than there is a desire to have a particular state of this file back to the workspace. To find out the commit id: git log

git checkout <ref id> <file>

You can use git reflog to find out the <ref id>. Reflog is a mechanism to record when the tip of branches are updated. This command is to manage the information recorded in it. Basically every action you perform with Git are recorded in the reflog.

Without specifying a <file>, you will get a "HEAD detached" state, see HEAD detached section for resolution.

Remote origin already exists

git remote rename origin old-origin

Bare repository

git init --bare

HEAD detached

git checkout -B master

Make HEAD your new local master.

If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset. This is the transactional equivalent of

$ git branch -f <branch> [<start point>]
$ git checkout <branch>

blame

git blame <file>

Tag

https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-tag

List tags:

git tag

Create tag:

git tag <tagname>

Pushing Tags to Remote:

git push origin v1.4

Sharing tags is similar to pushing branches. By default, git push will not push tags. Tags have to be explicitly passed to git push.

Deleting Tags:

git tag -d v1

Annotated Tags

git tag -a v1.4

Executing this command will create a new annotated tag identified with v1.4. The command will then open up the configured default text editor to prompt for further meta data input.

git tag -a v1.4 -m "my version 1.4"

Config

https://alvinalexander.com/git/git-show-change-username-email-address

Checking config

git config --list
git config user.name
git config user.email

Push

A handy way to push the current branch to the same name on the remote:

git push origin HEAD

Delete a branch remotely, in most cases the remote name is origin:

git push --delete <remote_name> <branch_name>

https://sethrobertson.github.io/GitFixUm/fixup.html#discard_all_unpushed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment