Sunday, January 21, 2024

git Quick Reference

The git versioning system is the de-facto standard for version control in the FOSS world and much of the software industry. However, its command-line interface can sometimes be hard to figure out, even for seasoned users. The list below is a quick command reference for common tasks:

Pulling Changes from Remote Repository

Pull repository data

git pull --rebase

Adding the --rebase option ensures that any local commits not yet pushed to the remote repo will be placed after any new pulled from it. This makes it possible to then push these commits without getting into any versioning conflicts.

Pull remote branch

git fetch origin remote_branch_name
git checkout -b remote_branch_name origin/remote_branch_name

Pushing Changes to Remote Repository

Push current branch to remote repository

git push

Push locally-created project into repository

git push --set-upstream <URL> master

Undoing Changes

Create a copy of a file’s previous version

git show HEAD^:$path_to_file > $path_to_copy

Unstage added files before commit

git reset HEAD

Restore a deleted file

Find the last commit that affected the given path. As the file isn’t in the HEAD commit, this commit must have deleted it.

git rev-list -n 1 HEAD -- <file_path>

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <deleting_commit>^ -- <file_path>

Or in one command, if $file is the file in question.

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

Wipe changes since last commit

git reset --hard

Remove untracked files / directories

git clean -f -d

Rollback last commit, returning changes to uncommitted state

git reset HEAD^

Tag Management

List tags

git tag

Create tag

git tag -a tag-name -m "tag description"

Push repository data with tags

git push --follow-tags origin master

Checkout tag

git checkout -b branch-name tag-name

No comments:

Post a Comment