Languages
[Edit]
EN

Git - how to create and push into new branch local changes?

10 points
Created by:
Kate_C
2892

Using Git sometimes it is necessary to make new branch after local changes has been made. In this article simple solution how to push changes into new branch has been described.

1. Branch creation steps

1.1. (Optionally) Check changes and list all branches:

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git branch -a

Note: readme.txt file has been found with available only master branch (git branch -a returned nothing).

1.2. Create new brach:

$ git checkout -b "my_new_brach_with_changes"
Switched to a new branch 'my_new_brach_with_changes'

1.3. (Optionally) Check active branch and changes:

$ git status
On branch my_new_brach_with_changes

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        readme.txt

nothing added to commit but untracked files present (use "git add" to track)

Note: active brunch name is my_new_brach_with_changes with listed changed/added files.

1.4. Add, commit and push changes:

$ git add -A

$ git commit -m "My first commmit to my new branch"
[my_new_brach_with_changes (root-commit) 5e87717] My first commmit to my new branch
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

$ git push --set-upstream origin my_new_brach_with_changes
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 240 bytes | 120.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To C:/Users/qurek/Desktop/git test/TestRepository.git/
 * [new branch]      my_new_brach_with_changes -> my_new_brach_with_changes
Branch 'my_new_brach_with_changes' set up to track remote branch 'my_new_brach_with_changes' from 'origin'.

Notes:

  • git add -A added all created/changed files to list to send,
  • git commit -m "My first commmit to my new branch" sent added files to local repository (my_new_brach_with_changes),
  • git push --set-upstream origin my_new_brach_with_changes created remote repository with same name (my_new_brach_with_changes) and sent local repository changes into remote one. --set-upstream set up remote reposotory where in future all local changes will be send - next time just git push is needed to send changes. origin describes default remote repository saved inside .git/config file.

1.5. (Optionally) Check active branch and changes:

$ git branch -a
  master
* my_new_brach_with_changes
  remotes/origin/my_new_brach_with_changes

$ git status
On branch my_new_brach_with_changes
Your branch is up to date with 'origin/my_new_brach_with_changes'.

nothing to commit, working tree clean

Note: to switch branch to master git checkout master should be used.

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.